Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an element in array exists in Java

If I have an int array structured like this:

private int[][] map = new int[400][400];

And I try to retrieve

map[100][200]

And that element isn't initialized, will i get a compiler/runtime error or will it return null? And is there any function to check if a given element/index exists/has been set?

like image 290
Ali Avatar asked Mar 07 '09 22:03

Ali


1 Answers

As your array declaration is of a primitive type you won't get any compiler or runtime errors - the default value of 0 will be returned.

If your array had been an array of Objects, then the array would hold null for any element not specifically assigned.

like image 71
Alnitak Avatar answered Oct 03 '22 13:10

Alnitak