next up previous index
Next: Length Up: Array Variables Previous: Initialisation of Array Elements   Index

Initialisation of Both Together

Arrays can be created and initialised in one operation using a list of values, as in
int[] a = {24, 65, 34, 96, 12};
This first allocates storage to the variable a for 5 integers, and secondly makes the corresponding assignments to the elements of the array that has been created. The length of the array is determined by the compiler at compile time in the obvious way. Note that new is not used explicitly when arrays are created by lists. Note also that initialiser lists can only be used when an array is declared. Thus
int[] a;
a = {24, 65, 34, 96, 12};
would be illegal.



Peter Williams 2005-06-07