我是Java新手,对于如何返回2个参数我很困惑。我应该做一个if陈述吗? 例如,对于下面的方法,我将如何编写return语句?还是我的构造函数?
public Cell getAir(int a, int b) {
return cells[a][b];
}
这是我的实际代码
private Air[][] pair;
public Grid(int width, int height) {
Air[][] pair = new Air[width][height];
width = width;
height = height;
if(width == 0 ||height == 0) {
} throw new IllegalArgumentException("Height or Width value is less than or equal to zero");
}
public Grid(Airl[][] cells) {
if(pair != null && pair.length > 0) {
for(int i=0; i < pair.length; i++) {
if(pair[I] == null || pair[I].length == 0)
{ throw new IllegalArgumentException("Width value is null or equal to zero"); } } }
else { throw new IllegalArgumentException("Height value is null or equal to zero"); }
}
public int getHeight() {
return pair[0].length;
}
public int getWidth() {
return pair[0].length;
}
public Air getAir(int a, int b) {
return air[a][b];
答案 0 :(得分:2)
通常的方法是定义一个包含两个值的类,然后返回作为该类实例的Object。
答案 1 :(得分:0)
返回对象。示例:
public Map.Entry<Integer, Integer> getAir(Cell cell) {
return new AbstractMap.SimpleEntry<>(cell.a, cell.b);
}
答案 2 :(得分:0)
只需使用javafx.util / Apache Commons Pair。像这样:
ImmutablePair<Integer, Integer> pair = new ImmutablePair<>(cell.a, cell.b);
Integer key = pair.getKey();
Integer value = pair.getValue();