Array Length in Java

Kiran Bhat picture Kiran Bhat · Jan 6, 2012 · Viewed 935.9k times · Source

I declared an array as shown below:

int[] arr = new int[10];

Then I assigned following values to the array:

arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;

Then I declared and initialized an integer variable:

int arrayLength = arr.length;

This will be useful to find actual size but is there any method to find logical size of the array?

Answer

Kai picture Kai · Jan 6, 2012

It contains the allocated size, 10. The unassigned indexes will contain the default value which is 0 for int.