How to Declare Arrays
An array is declared and created as follows:
int a; /* declares a reference 'a' to an interger
array of unspecified dimension */
a = new int; /* creates an array object with
3 interger elements */
The declaration and creation can be combined as follows:
This creates an array of integers as follows:
A different size of interger array object can be assigned to the array reference 'a' at any time as follows:
a = new int;
Java's built-in automatic garbage collection service gets rid of the old dis-referenced 3-element array.
The size or length of the object currently referenced by reference variable 'a' is always available in the length variable a.length. This is illustrated in the program below which prints out the contents of the array.
for(int i = 0; i < a.length; i++)
System.out.println(i + ": " + a[i]);
© 1998 Robert John Morton