Java – How to see if an element in an int array is empty



I want to see if array[5] holds a value or is empty.

Best Solution

Elements in primitive arrays can't be empty. They'll always get initialized to something (usually 0 for int arrays, but depends on how you declare the array).

If you declare the array like so (for example):

int [] myArray ;
myArray = new int[7] ;

then all of the elements will default to 0.

An alternative syntax for declaring arrays is

int[] myArray = { 12, 7, 32, 15, 113, 0, 7 };

where the initial values for an array (of size seven in this case) are given in the curly braces {}.