Multidimensional Array, Array Bound, Array Resizing and Copying Array

0




Multidimensional Arrays

The Java programming language does not provide multidimensional arrays in the same way that other languages do. Because you can declare an array to have any base type, you can create arrays of arrays (and arrays of arrays of arrays, and so on). The following example shows a two-dimensional array:

int[][] twoDim = new int [4][];

twoDim[0] = new int[5];

twoDim[1] = new int[5];

The object that is created by the first call to new is an array that contains four elements. Each element is a null reference to an element of type array of int and you must initialize each element separately so that each element points to its array.

Note – Although the declaration format allows the square brackets to be at the left or right of the variable name, this flexibility does not carry over to other aspects of the array syntax. For example, new int[][4] is not legal.

Because of this separation, you can create non-rectangular arrays of arrays. That is, you can initialize the elements of twoDim as follows:

twoDim[0] = new int[2];

twoDim[1] = new int[4];

twoDim[2] = new int[6];

twoDim[3] = new int[8];

Because this type of initialization is tedious, and the rectangular array of arrays is the most common form, there is a shorthand to create two-dimensional arrays. For example, you can use the following to create an array of four arrays of five integers each:

int[][] twoDim = new int[4][5];




Array Bounds

In the Java programming language, all array indices begin at 0. The number of elements in an array is stored as part of the array object in the length attribute. If an out-of-bounds runtime access occurs, then a runtime exception is thrown.

Use the length attribute to iterate on an array as shown in Code 1.

Code 1Using the Array Bounds to Iterate Over the Array

public void printElements(int[] list) {

for ( int i = 0; i < list.length; i++ ) { System.out.println(list[i]); } } Using the length attribute of the array makes program maintenance easier because you do not need to know the number of elements in the array at compile time.
Using the Enhanced for Loop

Iterating over an array is a very common task. The Java 2 Platform, Standard Edition (J2SETM) version 5.0 has added an enhanced for loop to

make array iteration easier. This is shown in Code.

Code 2 Using the Enhanced for Loop to Iterate Over the Array

public void printElements(int[] list) {

for ( int element : list ) {

System.out.println(element);

}

}

This version of the for loop can be read as for each element in list do. The compiler handles the iteration code. Code 2 is equivalent to Code 1.

Array Resizing

After it is created, you cannot resize an array. However, you can use the same reference variable to refer to an entirely new array:

int[] myArray = new int[6];

myArray = new int[10];

In this case, the first array effectively is lost unless another reference to it is retained elsewhere.

Copying Arrays

The Java programming language provides a special method in the System class, arraycopy(), to copy arrays. For example, you can use the arraycopy()method as follows:

1 // original array

2 int[] myArray = { 1, 2, 3, 4, 5, 6 };

3

4 // new larger array

5 int[] hold = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };

6

7 // copy all of the myArray array to the hold

8 // array, starting with the 0th index

9 System.arraycopy(myArray, 0, hold, 0,

10 myArray.length);

At this point, the array hold has the following contents: 1, 2, 3, 4, 5, 6, 4, 3,

2, 1.

Note – The System.arraycopy method copies references, not objects, when dealing with arrays of objects. The objects themselves do not change.