Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if an array element exists?

Tags:

java

I'm looking for Java's equivalent of PHP's isset();

int board[][]=new int[8][8];
...
if(isset(board[y][x]))
  // Do something with board[y][x]

Does such a function exist in Java?

Edit: Sorry, what I meant is that I want to check if board[100][100] exists or not. if(board[100][100]) would result in an array out of bounds error.

like image 522
Leo Jiang Avatar asked Sep 19 '25 23:09

Leo Jiang


1 Answers

In Java, int arrays are initialized to a value of zero, so you won't be able to tell if it's been not set, or if it's set to a value of 0.

If you want to check if it's set, you should use an array of Integer. If the value isn't set, it will be null.

Integer[][] board = new Integer[8][8];
...
if (board[x][y] != null) { ... }
like image 145
Jeffrey Blattman Avatar answered Sep 21 '25 14:09

Jeffrey Blattman