example:
I want to see if array[5]
holds a value or is empty.
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 {}
.
There is no such thing as an "empty" element in a Java array. If the array's length is at least six, then element 5 exists and it has a value. If you have not assigned anything else to that location, then it will have the value zero, just like an object's uninitialized field would have.
If it is an array of Object
descendants, then you can check whether the element is equal to null
.
You have to define what you mean by empty. Depending on the datatype of the array you can decide on the semantics of empty. For example, if you have an array of ints you can decide that 0 is empty. Or if the array is of reference types then you can decide that null is empty. Then you simply check by comparing array[5] == null or array[5] == 0 etc.
Primitive arrays (int, float, char, etc) are never "empty" (by which I assume you mean "null"), because primitive array elements can never be null.
By default, an int array usually contains 0 when allocated. However, I never rely on this (spent too much time writing C code, I guess).
One way is to pick a value that you want to treat as "uninitialized". It could be 0, or -1, or some other value that you're not going to use as a valid value. Initialize your array to that value after allocating it.
Object arrays (String[] and any array of objects that extend Object), can have null elements, so you could create an Integer[] array and initialize it to nulls. I think I like that idea better than using a magic value as described above.
Use objects rather than primitives.
Integer[] myArray …
… Objects.isNull( myArray[ 5 ] ) …
… Objects.nonNull( myArray[ 5 ] ) …
int[]
Integer[]
The Answer by Bill the Lizard is correct. An array of primitive values has no empty slots. All slots hold an element with a default value for that data type. For int
, the default is 0
, zero.
If you want to track empty slots, or “holes”, in your array, define the array as holding object references rather than primitive values. So you would want a Integer[]
rather than .int[]
Integer[] integers = new Integer[ 12 ] ;
System.out.println( Arrays.toString( integers ) ) ;
null
When run we see that all the elements are null
. This means no object references has yet been placed in that slot.
[null, null, null, null, null, null, null, null, null, null, null, null]
Let's assign some values. Java provides auto-boxing for automatically converting int
values into Integer
objects.
integers[ 2 ] = 42 ; // Auto-boxing.
integers[ 5 ] = Integer.valueOf( 99 ) ; // Unnecessary, because of auto-boxing.
System.out.println( Arrays.toString( integers ) ) ;
When run, we see two slots are used while ten remain unused (null
).
[null, null, 42, null, null, 99, null, null, null, null, null, null]
See this code run at Ideone.com.
You said:
I want to see if
array[5]
holds a value or is empty.
Test for null
.
boolean slotAtIndex5HoldsObjectRef = ( null != integers[ 5 ] ) ; // Parens are not necessary, but improve readability.
true
More elegant to use the Objects
utility class.
boolean slot5Filled = Objects.nonNull( integers[ 5 ] ) ;
true
Specify a default value if none assigned.
Integer x = Objects.requireNonNullElse( integers[ 5 ] , Integer.valueOf( 101 ) ) ;
Integer y = Objects.requireNonNullElse( integers[ 7 ] , Integer.valueOf( 101 ) ) ;
x: 99
y: 101
Create a constant to define the empty value, eg:
private static final int EMPTY = -1;
then create the array like this:
int[] myArray = new int[size];
Arrays.fill(myArray, EMPTY);
then to check if an element is 'empty', do this:
if (myArray[i] == EMPTY)
{
//element i is empty
}