19
Chapter 8: Arrays
An array is an indexed collection of variables, called elements. An array has a given length ℓ
≥
0 and a
given element type t. The elements are indexed by the integers 0, 1,...,ℓ - 1. The value of an
expression of array type u[] is either null or a reference to an array whose element type t is a
subtype of u. If u is a primitive type, then t must equal u.
8.1 Array Creation and Access
A new array of length ℓ with element type t is created (allocated) using an array creation expression:
new t[ℓ]
where ℓ is an expression of type int. If type t is a primitive type, all elements of the new array are
initialized to 0 (when t is byte, char, short, int, or long) or 0.0 (when t is float or double) or
false (when t is boolean). If t is a reference type, all elements are initialized to null.
If ℓ is negative, then the exception NegativeArraySizeException is thrown.
Let a be a reference of array type u[], to an array with length ℓ and element type t. Then
a.length of type int is the length
ℓ
of a, that is, the number of elements in a.
The array access expression a[i] denotes element number i of a, counting from 0; this
expression has type u. The integer expression i is called the array index. If the value of i
is less than 0 or greater than or equal to a.length, then exception
ArrayIndexOutOfBoundsException is thrown.
When t is a reference type, every array element assignment a[i] = e checks that the
value of e is null or a reference to an object whose class C is a subtype of the element
type t. If this is not the case, then the exception ArrayStoreException is thrown. This
check is made before every array element assignment at run-time, but only for reference
types.
8.2 Array Initializers
A variable or field of array type may be initialized at declaration, using an existing array or an array
initializer for the initial value. An array initializer is a comma-separated list of zero or more expressions
enclosed in braces { ... }:
t[] x = { expression, ..., expression }
The type of each expression must be a subtype of t. Evaluation of the initializer causes a distinct new
array, whose length equals the number of expressions, to be allocated. Then the expressions are
evaluated from left to right and their values are stored in the array, and finally the array is bound to x.
Hence x cannot occur in the expressions: it has not been initialized when they are evaluated.
Array initializers may also be used in connection with array creation expressions:
new t[] { expression, ..., expression }
Multidimensional arrays can have nested initializers (example 14
). Note that there are no array
constants: a new distinct array is created every time an array initializer is evaluated.
Example 10: Creating and Using One-Dimensional Arrays
The first half of this example rolls a die one thousand times, then prints the frequencies of the outcomes.
The second half creates and initializes an array of String objects.
int[] freq = new int[6]; // All initialized to 0
for (int i=0; i<1000; i++) { // Roll dice, count frequencies
int die = (int) (1 + 6 * Math.random());
freq[die-1] += 1;
}
for (int c=1; c<=6; c++)
System.out.println(c + " came up " + freq[c-1] + " times");
String[] number = new String[20]; // Create array of null elements
for (int i=0; i<number.length; i++) // Fill with strings "A0", ..., "A19"
number[i] "A" + i;
for (int i=0; i<number.length; i++) // Print strings