Arrays in Java III: Multidimensional Arrays

Multidimensional arrays are nothing but arrays of arrays. A multidimensional array is an array whose elements themselves are an array. In the previous two lessons about introducing arrays and initializing arrays, we saw that arrays may contain primitive data types or reference data types. Since, an array is a reference data type, it is allowed to encapsulate other arrays. This containment may run arbitrarly deep.

What is a Multidimensional Array?

A multidimensional array is not a different data structure. It is simply an array whose elements also happen to be array. A two dimensional array is an array whose each element is also an array. Similarly, a 3-D array is an array each of whose element is a 2-D array.

Here is a pictorial depiction of memory layout of a 1-D array

As you can see in the diagram above, the 1 d array has string values located side by side. All Java arrays, immaterial of their dimensionality,  start there index at 0 and so for the following we have –

Now, if we have an array where each of the above values shown above are themselves 1-D arrays, we get a 2 dimensional array. A 2D array has row numbers and column numbers, both beginning at 0. A row of a 2-D array is specified before the column. For example if you have a 2-D array called arr2D having 3 rows and 6 columns, its first row (which is actually a 1-D array) can be accessed as arr2D[0]. The third element of the first row would be arr2D[0][4].

The diagram below shows the memory layout of a 2 dimensional array with three rows and 6 columns.

Declaring a Multidimensional Array

Pretty much like 1-D arrays, you need to either  initialize your array at declaration time or include the size of the array in declaration.

Here is a code snippet showing various ways of declaring a 2D Java array

123456// Various ways of declaring a 2D array String[][] arr1;String arr2[][];String[] arr3[];String[][] arr4;

It doesn’ t matter whether you place the brackets after the type or variable name, the total should add to the dimensionality of the array.

You need 2 brackets for a 2 dimensional arrays and a 3 for dimensional arrays. Java allows you to distribute these over your type name and variable name.
For an n-dimensional there will be n-P-2 ways of declaring an array.

Initializing a Multidimensional Array

Initializing and instantiation of a multidimensional array follows the same basic guidelines that are required of 1 D array. Here is a code snippet demonstrating initialization of a 2-D array of integers.

You can initialize higher dimensionality arrays by nesting the array initializer blocks inside each other.

Example of Multidimensional Array

If you find yourself using an array of dimension 3 or more, it is time to stop coding and go back to see if the design is flawed.

2D arrays are used as matrix and are quite useful. But as a Java programmer,  you need to assess your situation if you find a higher dimensionality array in your code. Nevertheless, for sake of completeness, here is a sample code showing how to declare and initialize a three dimensional array –

Having looked at multidimesionality arrays, in the next lessons we will deal with manipulating individual array elements and iterating over arrays.3

Leave a comment

Your email address will not be published.