Previous | Table of Contents | Next |
14.2.9.2. Multidimensional Arrays
Java also supports multidimensional arrays. These are implemented as arrays-of-arrays, as they are in C. You specify a variable as a multidimensional array type simply by appending the appropriate number of [] pairs after it. You allocate a multidimensional array with new by specifying the appropriate number of elements (between square brackets) for each dimension. For example:
byte TwoDimArray[][] = new byte[256][16];
This statement does three things:
When allocating a multidimensional array, you do not have to specify the number of elements that are contained in each dimension. For example:
int threeD[][][] = new int[10][][];
This expression allocates an array that contains 10 elements, each of type int[][]. It is a single-dimensional allocation, although when the array elements are properly initialized to meaningful values, the array will be multidimensional. The rule for this sort of array allocation is that the first n dimensions (where n is at least one) must have the number of elements specified, and these dimensions may be followed by m additional dimensions with no dimension size specified. The following is legal:
String lots_of_strings[][][][] = new String[5][3][][];
This is not:
double temperature_data[][][] = new double[100][][10]; // illegal
Multidimensional arrays can also be allocated and initialized with nested initializers. For example, you might declare the following multidimensional array of strings for use by the getParameterInfo() method of an applet:
String param_info[][] = { {foreground, Color, foreground color}, {background, Color, background color}, {message, String, the banner to display} };
Note that since Java implements multidimensional arrays as arrays-of-arrays, multidimensional arrays need not be rectangular. For example, this is how you could create and initialize a triangular array:
short triangle[][] = new short[10][]; // A single-dimensional array for(int i = 0; i < triangle.length; i++) { // For each element of that array triangle[i] = new short[i+1]; // Allocate a new array for(int j=0; j < i+1; j++) // For each element of new array triangle[i][j] = (short) i + j; // Initialize it to a value }
You can also declare and initialize non-rectangular arrays with nested initializers:
static int[][] twodim = {{1, 2}, {3, 4, 5}, {5, 6, 7, 8}};
To simulate multiple dimensions within a single-dimensional array, youd use code just as you would in C:
final int rows = 600; final int columns = 800; byte pixels[] = new byte[rows*columns]; // access element [i,j] like this: byte b = pixels[i + j*columns];
14.2.9.3. Accessing Array Elements
Array access in Java is just like array access in Cyou access an element of an array by putting an integer-valued expression between square brackets after the name of the array:
int a[] = new int[100]; a[0] = 0; for(int i = 1; i < a.length; i++) a[i] = i + a[i-1];
Notice how we computed the number of elements of the array in this exampleby accessing the length field of the array. This is the only field that arrays support. Note that it is a constant (final) fieldany attempt to store a value into the length field of an array will fail.
In all Java array references, the index is checked to make sure it is not too small (less than zero) or too big (greater than or equal to the array length). If the index is out of bounds, an ArrayIndexOutOfBoundsException is thrown.7 This is another way that Java works to prevent bugs (and security problems).
7The discussion of exceptions and exception handling is still to come.
Previous | Table of Contents | Next |