Haris Ghauri Haris Ghauri - 1 month ago 6
Java Question

How to know if an object of type (lets say ClassA) exists at a specific location in a multidimensional array in java?

How to check if the object of ClassA exists in a multidimensional array grid[x][y] ?

private Object grid[][];
...

...
...


public boolean isClassA(int x, int y){
return grid[x][y] == ClassA.class;
//what should I return here?
}

Answer

You can use the instanceof operator. For example:

public boolean isClassA(int x, int y){
    return (grid[x][y] instanceof ClassA);
}

More documentation on this keyword: http://www.java2s.com/Tutorial/Java/0060__Operators/TheinstanceofKeyword.htm

Comments