An array is a sequence d0, d1, d2, ..., dn-1, n>=0 of data values of fixed length n . In contrast to more general representations of sequences, an array cannot grow or shrink. It has a length n>=0 that is specified when it is created which it retains for the duration of its existence.
In Scheme, an arrays is called vector. In contrast, Java and most other languages use the term array. Java arrays are almost identical to Scheme vectors; the only significant difference is that every array in Java has a declared type T[] asserting that all elements of the array have type T. All of the primitive operations on arrays Java preserve the declared types attached to arrays.
In Java, arrays of every type are built into the language. If you define a class C, then the array type C[] is automatically supported by Java. Every array type is a subtype of type Object, but array types cannot be extended. In other word, each array type is final. Array indexing begins at 0 as in Scheme, C, and C++. Hence an array length nhas valid indices of 0, 1, ..., n-1.
Every Java array has a int field length that contains the length of the array. If a Java variable has type T[] for some primitive or object type T then it can only be bound to arrays of type U[] where U is any subtype of T (including T of course).
A Java array value is actually a reference to an array. Hence, a variable of type T[] in Java can have the value null. In addition, two variables can be bound to exactly the same array. In fact, the expression x.equals(y) is true iff x and y refer to the same array, i.e., x == y. Scheme variables bound to vectors behave in exactly the same way.
Arrays are allocated in Java using the new statement just like other Java objects. The array form is
new T[length]where T is any type and length is an expression that evaluates to a non-negative int. Arrays can have length 0. Each element of the array is set to the `zero'' value of the declared type T. If T is an object type, the initial value of each element is null. Hence, the expression
new String[1000]allocates an array of 1000 String elements all bound to null. Similarly, the expression
new int[10]allocates an array of 10 int elements all bound to 0. The expression
new String[0]allocates an array of zero String elements.
Java has an alternate form for constructing an array called an anonymous array. The expression
new T[] { v0, ... vn-1}allocates an array of length n of type T containing the specified sequence of elements.
The element values stored in a Java array can be modified by assignment
operations. In essence, each element of an array is a cell containing
a value.
Given an expression A denoting an array of type T[] of length n,
an expresson i denoting an int in the range
,
and an expression e of type T,
the statement
A[i] = e;changes the contents of the cell with index i to e. For example, if int[] a is bound to the array created by the expression
new int[10]the statement
a[1] = 100;changes the contents of element 1 from 0 to 10. Similarly, the loop statement2.1
changes the element values of the array a to match the contents of the arrayfor (int i = 0; i < 10; i++) a[i] = 10*i;
new T[] { 0, 10, ... 90}However, after executing the preceding loop, the expression
a.equals(new T[] { 0, 10, ... 90})returns false. The array object a is distinct from the array object constructed by the expresion
new T[] { 0, 10, ... 90}even though they both contain the same element values!
Java uses two mechanisms to enforce the declared type T[] of an array object. First, when an array T[] is allocated, all of the initial element values must be of type T. Second, when an array object of type T[] is updated by an assignment to an element of the array, the new value stored in the array must be of type T.
Java array types have an interesting property called co-variance:
an array variable of type T[] can be bound
to an array A of type T'[] provided
.
As a result, the array type Object[]
is a supertype for all object array types, which permits
arrays to be treated polymorphically in some situations.