Ad

How Can I See If An Element In An Int Array Is Empty?

- 1 answer

example:

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

Ad

Answer

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 {}.

Ad
source: stackoverflow.com
Ad