Multi Dimensional Array in Java

The Multi Dimensional Array in Java programming language is nothing but an Array of Arrays (Having more than one dimension). In our previous article we discussed about Two Dimensional Array which is the simplest form of Java Multi Dimensional Array.

In Java Programming Language, we can declare n-dimensional array or Muti dimensional array by placing n number of brackets [ ], where n is dimension number. For example,

int[2][3][4] StudentArray = Three Dimensional Array

int[2][2][3][4] StudentArray = Four Dimensional Array

In this article we will show you, How to declare, initialize Multi Dimensional Array in Java. For better understanding we are using Three Dimensional Array and you can try four dimensional array using the same technique

Declaration of Multi Dimensional Array in Java

Following code snippet will show you the declaration of multi dimensional array in Java Programming Language:

Data_Type[][][] Array_Name;

Data_type: This will decide the type of elements it will accept. For example, If we want to store integer values then, the Data Type will be declared as int, If we want to store Float values then, the Data Type will be float etc

Array_Name: This is the name you want to give it to this Multi Dimensional Array in Java. For example Car, students, age, marks, department, employees etc

Similarly you can declare the remaining type of Multi Dimensional Array in Java:

Creating Multi dimensional Array in Java

In order to create a multi dimensional array in Java, we have to use the New operator as we shown below:

Data_Type[][][] Array_Name = new int[Tables][Row_Size][Column_Size];

If you observe the above code snippet of this Java Multi Dimensional Array,

Tables: It will decide the number of tables an array can accept. Two Dimensional Array is always single table with rows and columns where as Multi Dimensional array in Java is more than 1 table with rows and columns.

Row_Size: Please specify the number of Row elements an array can store. For example, Row_Size =5 then array will have 5 rows.

Column_Size: Number of Column elements an array can store. For example, Column_Size = 6 then array will have 6 Columns.

Here, We did not mention the data level, row size and column size but the compiler is intelligent enough to calculate the size by checking the number of elements inside the row and column.

Third Approach

Above two ways are good to store small number of elements into the array, What if we want to store 100 rows or 50 column values. It will be a nightmare to add all of them using any of the above mentioned approaches. To resolve this, we can use the Nested For Loop in Java concept here:

Here we declared a three dimensional array of size 2 levels * 5 rows * 3 columns but we only assigned values for one row. In this situation, the remaining values will be assigned to default values (0 in this case).

Accessing Multi Dimensional Array Elements

In Java programming, We can use the index position to access the Java multi dimensional array elements. Using index we can access or alter/change each and every individual element present in multidimensional array.

Index value of Multi Dimensional Array in Java starts at 0 and end at n-1 where n is the size of a tables, row or column. For example, an int[][][] Array_name = new int[2][6][4] will allow us to store maximum 2 levels of data (rows and columns), 6 row elements and 4 column elements. To access or alter 1st value use Array_name[0][0][0], to access or alter 2nd row 3rd column value at 1st level then use Array_name[0][1][2] and to access the 6th row 4th column in 2nd level then use Array_name[1][5][3]. Lets see the example for better understanding:

Below For loop will help to iterate each and every cell present in both a and b array. Condition inside the for loops (rows < a[0].length) will ensure the compiler, not to exceed the array row limit and (columns < a[1].length) will ensure the compiler not to exceed the array column limit.

TIP: a.length is used to find the level of data (first dimension), a[0].length is used to find the length of the rows (second dimension) and a[1].length is used to find the length of the columns (third dimension).

The value of columns is 3 and the condition (columns <= 3) is True. Since we didn’t exit from the inner loop (Columns loop), Row value will be 0Sum[0][0][3] = a[0][0][2] + b[0][0][2];

Sum[0][0][3] = 8 + 40;

Sum[0][0][3] = 48;

From the fourth iteration, The value of columns became 4 and the condition (columns <= 3) will fail. So it will exit from the loop. Now the value of rows will be incremented by 1 and starts the second row iteration

Row Second Iteration

The value of row will be 1 and the condition (rows < 2) is True. So, it will enter into second for loop

Column First Iteration

The value of column is 0 and the condition (columns <= 3) is True. So, it will start executing the statements inside the loop until the condition fails.

After the increment the value of columns will be 4 and the condition (columns <= 3) will fail. So it will exit from the Column For loop. Next, the value of rows will be incremented by 1, it means rows = 2. Condition (rows < 2) will fail So, it will exit from the Row loop.

Now, the value of tables will be incremented by 1, it means tables = 1 and the Condition (tables < 2) is TRUE So, it will start executing Tables Second Iteration. Repeat the above process for one more time to get the remaining results.

Next for loops will traverse as we explained above but instead of summing it will display the values one by one with tab separation using the system.out.format statement inside them.