Multi Dimensional Arrays
We have seen that an array can hold references to objects and an array itself is a reference type so an array of arrays seems quite plausible. These are referred to as multidimensional arrays. We can arrays of two, three or more dimensions. We will look at multi dimensional arrays, with special focus on two dimensional arrays which are quite frequently used. They are generally used to represent matrices. But unlike a mathematical matrix, there are a few differences which we will see now.
We have represented an array type using a pair of brackets. Two dimensional arrays are in a similar way represented by two such pairs of brackets and an N dimensional array is represented by using N such pairs of brackets. The following statement creates a two dimensional array of integers, which contains 3 arrays containing 4 integers each.
int[][] a=new int[3][4];
We can also think of this array as a 3*4 matrix in mathematics, with each row of the matrix being a Java array. You might also think of it in the other way, also as a 4*3 matrix, but we generally use the first notation.
Elements of this array are accessed by specifying the index numbers, here two of them. The first representing the array number and the second representing the index element in that particular array.
a[0][2] = 34;
A two dimensional airy can also be initialised using an array initialiser in the following way. This paints a better picture of a 2D array as an array of arrays.
int[][] d = { { 1,5,74,2}, {4,68,45,65},{5,0,34,54}}:
The above array a[][] contains three arrays { 1,5,74,2}, {4,68,45,65} and {5,0,34,54}. This particular 2D array contains equal number of elements in each of its sub arrays. We can, if we wish to, create 2D arrays which have different number of elements in its sub arrays.
int[][] c = { { 4,56,7}, {1}, {45,78} };
The above two dimensional array contains three arrays. The first of these has three elements, the second has a single element and the last of these has two elements. We can also create such non symmetric arrays using the new keyword in the following way:
int[][] b = new int[3][];
b[0]=new int[3];
b[1]=new int[1];
b[2]=new int[2];
The above three sets of statements also creates an array space b identical to that of a created earlier using an array initializer. Observe that in the first statement, we haven't specified any integer in the second pair of square brackets following [3]. b[][] is reference type and so are b[0], b[1] and b[2]. However, b[0][0], b[0][1] .... are of primitive type int. These things are to be dealt with carefully when we pass an array, a sub array or an element of the array as parameters.
The length of a two dimensional or a multidimensional array gives the number of arrays it contains. For example: b.length gives 3 while b[0].length gives 3, b[1].length gives 1 and so on. All these facts are easy to assimilate if we consider two dimensional arrays to be an array of arrays rather than as a mathematical matrix.
We manipulate multidimensional arrays using nested loops. For example, the following code snippet is used to print the elements of an array as a matrix. The code works even when the rows of the array are of different lengths.
for ( int i=0; i<a.length; i++) {
for(int j=0; j< a[i].length; j++)
System.out.print ( a[i][j] );
System.out.println();
}
Searching a two dimensional array is performed using a linear search. The labelled break statement is used to break out of the loop once the element is found.
outer:
for ( int i=0; i<a.length; i++) {
for(int j=0; j< a[i].length; j++) {
if ( a[i][j] == target) {
System.out.println("Element found at ["+i+"]["+j+"]");
break outer;
}
}
}
Sorting isn't generally performed on multidimensional arrays since we generally use them to represent a physical situation rather than as data structures to hold information. For example, we may use a three dimensional array to hold Student objects in a more realistic way. We have different cities and each city has different schools which has students. Let each city, school and Student have a code. A Student object in that case can be accessed more conveniently as s[city code][school code][student code]. This 3D representation gives a more realistic picture than a 1D array of Students.
