# Multidimensional Arrays in Java

Multidimensional arrays are arrays of arrays. For instance, a 2D array is an array of 1D arrays and a 3 D array is an array of 2 D arrays, where each of those 2D arrays is again an array of 1D arrays.

### Multidimensional array declaration and initializations

Declaration without initialization

int mArray[][];  or

int mArray1[][][]; or

int [] mArray2[][];

Initializing by specifying sizes

We can initialize a multidimensional array the same way as a single dimensional array specifying the sizes for each dimensions.

Below example declare and initialize a 2-D array with 2 rows and 3 columns.

int matrix[][] = new int ;

This will create a matrix (or 2-Dimensional array) with 2 rows and 3 columns as:

0 0 0
0 0 0

Initializing using short hand notation

You can also initialize a 2D array with custom constant values similar to a 1D array using an initializer statement.

int [] [] mArray = {{0, 1, 2,3}, {4, 5, 6,7}}; ​

Resulting array will be:

0 1 2 3

4 5 6 7

Ragged array declarations and assignment

A 2-D array is an array of 1-D arrays.

In memory, there will be a one-dimensional array and each element of this array references a one-dimensional array representing each row. Every row can have different number of columns i.e. different row lengths. Arrays of this type are called ragged arrays.

You can define a ragged array either using an initializer with different length contents for each rows or by creating a 2-D array specifying only the number of rows and then creating individual row arrays as below:

int matrix[][]= new int[]; // we are not specifying column size

matrix= new int; //column size for row 1 (index 0)

matrix= new int; //column size for row 2 (index 1)

Note that we gave only the number of rows in the initializer and then gave different column lengths for different rows separately. Above matrix will now look as:

0 0 0
0 0 0 0

Points to Remember

1. Multidimensional arrays are arrays of arrays.

• Verify using the getClass().isArray() methodSystem.out.print(arr.getClass().isArray()); will return true for a valid multi dimensional array.

2. Number of brackets in declaration (left side) correspond to the dimension.

• We can have the brackets anywhere within the declaration, combine declaration and initialization etc.

3. We use nested curly braces. Number of nestings denote the dimension: { } for ID, {{ }} for 2D. {{{ }}} for 3D etc.

4.  You can specify empty arrays or arrays with 0 size.

• int[ ] i[ ] = { { 1, 2 }, { 3 }, { } } ;

5. You can also initialize a sub array within a multidimentional array as null.

• int[ ] i[ ] = { { 1, 2 }, { 3 }, null } ;

6. You can declare a sub array of a multidimensional array using new keyword within a short hand initializer:

• char cA[][] = { new char[ ]{ 'a', 'b', 'c' } , new char[ ]{   'a', 'b', 'c' } };

• char cA[][] = { new char[ ]{ 'a', 'b', 'c' } , new char};

7. Even when you declare the sub arrays, you have to either specify size or use short hand:

• char cA[][] = { new char[ ]{ 'a', 'b', 'c' } , new char{   'a', 'b', 'c' } }; is NOT legal.

8. Multidimensional arrays in Java are ragged arrays.

1. Different rows can have different length.

2. Length of each row can be found as aa.length, arr.length etc.

### Accessing elements of multidimensional array

Similar to single dimensional arrays, we can access values in a single dimensional array using indexes.

Consider below array:

0 1 2 3

4 5 6 7

We can access the elements of above array as:

mArray will give 0.

mArray will give 1.

mArray will give 2.

mArray will give 4.

mArray will give 5.

and so on.

Similar to 1-D arrays, we will get an ArrayIndexOutOfBounds exception if we try to access a non-existent array location.

mArray will give exception as there are only two rows and their indexes are 0 and 1.

mArray will give exception as there are only four columns and their indexes are 0,1,2 and 3.

A 2-D array is an array of 1-D arrays. Try to guess the output for below:

System.out.print (mArray.length);

System.out.print (mArray.length);

System.out.print (mArray);

Output will be

4

4

6

mArray represent the first row array and hence mArray.length will be 4.

mArray represent the second row array and hence mArray.length will be 4. In Java, different rows can be of different lengths.

mArray is the third element in second row, which is 6. Note that array indexes in java are 0-based.

### Traversing a 2D array

We can make use of a nested for loop to traverse a 2-D array:

for(int i=0;i<ROWS;i++)

{

for(int j=0;j<COLS;j++)

{

System.out.print(" "+matrix[i][j]);

}

System.out.println();

}

Similarly we can traverse a ragged array as:

for(int i=0;i<matrix.length;i++)

{

for(int j=0;j<matrix[i].length;j++)

{

System.out.print(" "+matrix[i][j]);

}

System.out.println();

}

Though not very common, we can have arrays with more dimensions than 2. Consider an example of a 3D array initializer:

int [][][] arr3D = {{{1,2},{3,4}},{{1,2},{3,4}}};

Note the extra curly braces for a 3D array. There will be two level nesting of curly braces for a 3D array whereas 1 level of nesting for a 2D array ( e.g. {{0, 1}, {2, 3}, {4, 5}} ) and no nesting of braces for a 1D array (e.g. {1,2}).

You can see that 3D arrays are arrays of arrays of arrays. You can also initialize them as:

int [][][] arr3D1 = new int ;

int[] arr1D={1,2};

arr3D1=arr1D;

arr3D1=arr1D;

...