10

example:

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

sblundy
  • 60,628
  • 22
  • 121
  • 123
kylex
  • 14,178
  • 33
  • 114
  • 175

6 Answers6

22

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

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • Just as an aside, I believe (though I am not 100% on this) that int[] myArray = new int[] {12, 7, 32, 15, 113, 0, 7}; is also allowed. That is, I believe you are allowed (though you do not have to) to put new int[] in front of your created array. – MetroidFan2002 Nov 13 '08 at 04:20
  • @MetroidFan2002: Yes, that syntax is also valid. – Bill the Lizard Nov 13 '08 at 13:46
  • 1
    If you want Null values, you should declare it as Integer [] array. The problem with using int[] is that you can't tell the difference between someone assigning a 0 value, and it having defaulted to that. If this is OK, then you can use the primitive. If you need to know the difference, you'll have to use Integer. – Matt Jul 15 '12 at 18:57
7

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.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • what is stored in element 5 if nothing has been assigned to it then? – kylex Nov 13 '08 at 03:20
  • Integers are initialized to 0 by default. –  Nov 13 '08 at 03:22
  • 1
    it's dependent on the array's component type. for all numerical primitives, it's 0. For char it's ascii code 0 (aka the null char). For byte, it's byte value 0. For boolean it would be false. And for any non-primitive (ie an Object), it's null. – Matt Jul 15 '12 at 19:00
3

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.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
2

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.

Terry Lacy
  • 31
  • 2
0

tl;dr

Use objects rather than primitives.

Integer[] myArray …
… Objects.isNull( myArray[ 5 ] ) …
… Objects.nonNull( myArray[ 5 ] ) …

Use objects, not primitives ➠ 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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-1

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
}
Leigh
  • 4,268
  • 1
  • 27
  • 17
  • It's tough to come up with a constant that you can guarantee will never be a valid value that could legitimately be put in the array. If you really have to know whether anything has been added, probably better to use an array of Integers and check for null. – Jacob Mattison Nov 13 '08 at 14:56
  • Yeah that's a fair point, but I find in practice there is usually a range of valid integers that will be added to the array, so for example, a negative integer, or Integer.MAX/MIN_VALUE, can be used as EMPTY. – Leigh Nov 13 '08 at 16:23