In Java, an array is a collection of values that are of the same primitive type or of the same reference type. Since an array type is itself a reference type, arrays of arrays can be constructed.
The numbers of the days in a non-leap year on which each of the twelve months start are 1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305 and 335. For example, March 1st is the 60th day of the year. Suppose we want to provide an array called monthStarts that contains this information.
In Java, there are two syntaxes for an array declaration. To keep C programmers happy, an array declaration can be written using the following syntax:
0132: int monthStarts[ ];However, the following syntax is better:
0133: int[ ] monthStarts;
Note that the number of elements in the array is not included in this declaration.
This is because this declaration only declares a reference variable
that can be used to refer to an array object.
In order to create the actual array object we need to use an
assignment statement that contains an
array creation expression
on its RHS:
0134: monthStarts = new int[12];
So this has set up
monthStarts
to be a reference variable that refers to an array of 12
integers,
with indexes from 0 to 11.
You can access each individual element using the usual notation:
0135: monthStarts[0] = 1; 0136: monthStarts[1] = 32; 0137: ... 0138: monthStarts[11] = 335;
If an index is out of bounds, the exception ArrayIndexOutOfBoundsException will be thrown: details about ‘exception handling’ are given later.
As before, the creation expression can be used as an initializer:
0139: int[ ] monthStarts = new int[12]; 0140: monthStarts[0] = 1; 0141: monthStarts[1] = 32; 0142: ... 0143: monthStarts[11] = 335;This code can be abbreviated to:
0144: int[ ] monthStarts = { 1,32,60,91,121,152,182,213,244,274,305,335 };
Suppose we want an array where each element is an object of class java.awt.Point. Perhaps we want an array to represent the four vertices of the rectangle (100,100), (300,100), (300,400) and (100,400). We can do this as follows:
0145: Point[ ] vertices = new Point[4]; 0146: vertices[0] = new Point(100,100); 0147: vertices[1] = new Point(300,100); 0148: vertices[2] = new Point(300,400); 0149: vertices[3] = new Point(100,400);
0150: Point[ ] vertices = { new Point(100,100), new Point(300,100),
0151: new Point(300,400), new Point(100,400) };
Within the square brackets of an array creation expression, there needs to be an expression indicating the number of elements that are required in the array object. This expression may be one whose value is not known until runtime. For example:
0152: BufferedReader input = ... ; 0153: int size = Integer.parseInt(input.readLine()); 0154: int[ ] monthStarts = new int[size];
Having created an array object, the size of the array object is fixed. Suppose you are storing details about a collection of people, and suppose the size of the collection changes during the course of the execution of a program. It may be that you have no idea what the maximum size of the collection will be. Although you could arbitrarily choose a large value, this is wasteful of space, and no matter what value you choose, your program will fail if the value you choose is too small. In such situations, it is probably better to use a List, a Set or a Map. These are facilities that are provided by the Collections API of the Java 2 Platform. For more details, see ITS Guide 108 Advanced Java.