How can I check if an array element exists?
I'm looking for the Java equivalent of PHP isset () ;
int board[][]=new int[8][8];
...
if(isset(board[y][x]))
// Do something with board[y][x]
Does such a feature exist in Java?
Edit: Sorry, I meant I want to check if it exists board[100][100]
or not. if(board[100][100])
will result in an out of bounds error.
source to share
In Java, arrays are int
initialized to zero, so you won't be able to tell if it wasn't set or set to 0.
If you want to check if it is set, you must use an array Integer
. If the value is not set, it will be null
.
Integer[][] board = new Integer[8][8];
...
if (board[x][y] != null) { ... }
source to share
if (board[x][y] != null) {
// it not null, but that doesn't mean it "set" either. You may want to do further checking to ensure the object or primitive data here is valid
}
Java has no equiv. to isset because knowing whether or not something is actually set goes beyond just adding a value to a location.
source to share