next up previous
Next: Expressions that denote array Up: Unit 07 Previous: Access to the single

Number of elements of an array: instance variable length

Each array object contains, besides the collection of elements, a public instance variable (non modifiable) called length that stores the number of elements of the array. Hence, by accessing the variable length it is possible to obtain the number of elements of an array.

Example:

double[] v;
v = new double[5];
System.out.println(v.length); // prints 5

For brevity, we will in general not show the instance variable length when illustrating array objects, knowing that it is always present, as in the precedent figure.


next up previous
Next: Expressions that denote array Up: Unit 07 Previous: Access to the single