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 –
1 2 3 | arr1D [0] = "http://", arr1D [1] = "Coding"; arr1D [2] = "Coding"; arr1D [3] = ".com"; |
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; |
1 2 3 4 5 | // 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.
1 2 3 4 5 6 7 8 9 10 | int[][] ints = { // beginning of an array initializer block {1, 2, 3, 4}, // a 1-D array initializer block {5, 6, 7, 8}, // another 1-D array initializer block {9,10,11,12} }; System.out.println(ints[2][0]); // prints 9 |
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 –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // A 3D array having 2 2D arrays int[][][] moreInts = { // this is a 2d array at position 0{ // this is a 1d array in position 0 // of first 2d array {100, 200, 300}, // this is a 1d array in position 1 // of first 2d array {400, 500, 600} }, // this is a 2d array at position 1 { // left blank to demonstrate that // arrays can be irrelegular} }; |
Having looked at multidimesionality arrays, in the next lessons we will deal with manipulating individual array elements and iterating over arrays.3