Witscale Test Center

7.4 Arrays > 7.4.5 Multidimensional array


7.4.5 Multidimensional array

A multidimensional array is an array of array references. Since an array object can store object references, it can store references to other arrays. Thus, it can declare an array of arrays. The multidimensional arrays are either regular or non-regular arrays. A regular array’s elements are array references to the arrays of equal size. Conceptually it is a matrix-like structure where number of rows and columns are known.

 

The figure 7.8 shows how a regular two-dimensional array of integers can be declared and constructed.


7.8 A regular multidimensional array of primitive integers

 

The matrix is two-dimensional array of three array references. Each array reference points to an array of fixed size 2. However the matrix array need not  know the size of these arrays. In fact, at the time of creating multidimensional array, only the first dimension is required. Java does not require the second dimension. So the matrix is created as new int[3][]; specifying that it is a array of 3 array references.

Non-regular multidimensional arrays

 As we saw in the previous section, a multidimensional array contains array references. Let us call the arrays referenced by these array references as the sub-arrays. In any multidimensional array, each sub-array is a separate array object and may be of any size. The sub-array of matrix in figure 7.8 are the int arrays of size 2..  But each sub-array can actually be of any size; a multidimensional array with sub-arrays of varying sizes is a non-regular one.  The following code shows two different ways of creating a non-regular multidimensional array:

 

int [][] multidimensionalArr  = {{2,7},{3,1,7}};

int[] multi[] = { new int[2], {4,8,9,3}};

 

The multidimensionalArr is a array of two array references. The first array reference points to an array {2,7} and the second points to {3,1,7}. The multi is another example. It is also an array of two array references . The first array reference points to an array of 2 integers with each element initialized to 0 and the second points to an integer array {4,8,9,3}

In figure 7.9, one more example of a non-regular array is elaborated. The multiArray is an array of five array references. The first element is assigned a reference to a one-dimensional integer array (of size 3) and second element is a reference to another one-dimensional integer array (of size 5).


 

 

 

 

 

Figure 7.9 Non-regular Multidimensional array declaration and initialization

 

The multiArray is not a regular structure like a mathematical matrix. A mathematical matrix is a regular two-dimensional structure for storing numbers. The numbers are organized in rows and column. For example, a matrix of 3x4 has three row and four columns. The multiArray is also two-dimensional like a matrix but it is not necessarily regular. We can tell that it has two rows but cannot say how many columns. In simple words, we can say it is an array of how many sub-arrays but we may not know what will be the size of each sub array.

 

Specifying size in multidimensional arrays

Multidimensional array’s elements are array references. These array references may further point to a one-dimensional array or two-dimensional array or so on. For example,

 

int[][][] multiDim  = new int[5][][];

 

The multiDim reference is assigned to an array of size 5. The other two [][] in new int[5][][] indicates that its elements are of type int[][]. You need to give the size only in the first brackets. You cannot specify a value in [] if the left side [] is empty.

 

int[][][] multiDim = new int[5][4][];    // valid

int[][][] multiDim = new int[5][][5];    // Invalid

 

The non-regularity of multidimensional arrays sometimes makes accessing or initializing them a bit difficult. However, the non-regularity also makes multidimensional arrays very flexible as you can declare array of array (of any size) of arrays (of any size) and so on, as long as you can track the elements properly.