1

So unlike the int array where 0 is used as a placeholder, are there any placeholders for unused array indexes? I cannot figure out for the life of me how to use a for to find the logical size of my array. Thank you!

JStef
  • 176
  • 1
  • 7
  • Hello, you may find the answer in this [post](http://stackoverflow.com/questions/9297899/where-is-arrays-length-property-defined) – Romain Feb 26 '16 at 15:35
  • 2
    Relying on array entry values like null or 0 as an indication for "not used" is something one should think about twice. Use a collection that can grow (e.g. ArrayList) to store just the relevant entries. If the index value is relevant (and you need "gaps"), consider using a Map. – laune Feb 26 '16 at 15:36

4 Answers4

3

The default value of an entry in an object-typed array (String[], Thread[], Object[], etc.) is null. That's the value analogous to your example of 0 in an int[] array.

E.g.:

Object[] objectArray = new Object[10];
System.out.println(objectArray[0] == null); // "true"

There's a difference between 0 in int[] and null in Object[], of course: 0 is a valid int you can use for all the things you can use ints for. null, of course, is an object reference that doesn't refer to any object, so virtually the only thing you can do with that object reference is see whether it's null. :-)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Determining the "logical" size of an array can be tricky, primarily because in all cases the array must have valid values present. There are no "placeholder" values that mean "there is no value here".

Arrays of primitive integer types (byte, char, short, int and long) are initialized to the default value for those types, 0, as you note.

Arrays of floating point types (float and double) are initialized to the default value for those types, 0.0.

Arrays of boolean are initialized to false.

And arrays of any Object type are initialized to null.

In all these cases, a variable of the appropriate type can take the default value. So how does one tell whether those are real values that just happen to equal the default, or default values that haven't actually been provided by the application? You have to define what "placeholder" means for your application, and make sure you initialize your arrays accordingly.

One thing you might do, if your arrays are meant to hold numbers, is make them the corresponding Object type, i.e. use Integer[] rather than int[]. This will cause the array to be initialized to null rather than 0. Any 0 appearing in such an array would be a legitimate 0 rather than an artifact of initialization.

If null is a legitimate value, you might consider using Optional<Foo>[] value = rather than Foo[] value =. Then any array element that is null has definitely not been set. If your value[n] != null, you could check value[n].isPresent() to determine whether there's a real Foo held there or not.

There are lots of possibilities here, but the bottom line is that you have to make up your own mechanism. Nothing like a "placeholder value" is built into Java.

Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38
0

arrayref.length is the size of the array. If un-unsed/un-assigned it will have null or ZERO based on type of array.

kosa
  • 65,990
  • 13
  • 130
  • 167
0
int [] a = new int [10];
a[0] = 1;
a[1] = 2;
int logicalLength = 0;
for(int i=0; i<a.length; i++){
    if(a[i] != 0){ // Here a[i] != 0 changes depending on the type of the array
        logicalLength++;
    }
}
System.out.println(logicalLength);
System.out.println(a.length);

In an array, he we don't assign any value, then the default value is stored. Default value for int is 0, double is 0.0 and String is null.

Nikitha
  • 373
  • 2
  • 18