0

I'm new to Java and I'm quite confused on how to return 2 parameters. Am I supposed to make an if statement? For example, for the method below how would I go about in writing a return statement? or maybe it's my constructor?

public Cell getAir(int a, int b) {        
     return cells[a][b];
}

this is my actual code

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];
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Fxx
  • 33
  • 3

3 Answers3

2

The usual way is to define a Class that contains the two values and then return an Object that is an instance of that Class.

Sven Affeld
  • 311
  • 3
  • 8
0

Return Object. Example:

public Map.Entry<Integer, Integer> getAir(Cell cell) {
        return new AbstractMap.SimpleEntry<>(cell.a, cell.b);
    }
Ilya
  • 720
  • 6
  • 14
0

Just use javafx.util/Apache Commons Pair. Like this:

ImmutablePair<Integer, Integer> pair = new ImmutablePair<>(cell.a, cell.b);
Integer key = pair.getKey();
Integer value = pair.getValue();
Lorelorelore
  • 3,335
  • 8
  • 29
  • 40