Multi-dimensional Array in Java Programming – In this article, we will brief in on all the possible ways to evaluate multi-dimensional arrays in Java Programming with sample program. In case if you have any doubts about this tutorial do leave a comment here.
All the methods will be explained with sample programs and suitable examples. The compiler has also been added so that you understand the whole thing clearly.
The methods used in this article are as follows:
- Using Standard Method
- Using For Loop
- Using Scanner Class
- Using String
An array, as we all know, is a collection of multiple elements of the same data type. Arrays are normally used to store information of one particular type of variable.
As the name of the title suggests, multi-dimensional means any particular entity having 3 or more than 3 dimensions in reality.
Similarly, a multi-dimensional array in Java usually has 3 or more defined indexes, but in reality, one particular row of elements have another multitude of elements defined on their names.
Basically, you can have a 3×3 or a bigger matrix of elements to define a multi-dimensional array.
As you can see as per the image uploaded above, firstly, you need to specify the number of rows and columns respectively.
The example roughly explains the structure of a 3×4 matrix.
So, the total number of elements that will be entered is 12.
The elements that are entered are as follows:
1 2 3 4 5 6 7 8 9 10 11 12
These elements will be arranged accordingly and the multi-dimensional array will be printed for you.
Thus, the numerous methods that are used to carry out the same task in Java are:
Multi Dimensional Array – Using Standard Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class MultidimensionalStandard { public static void main(String args[]) { int[][][] a={{{10,20},{30,40}},{{50,60},{70,80}}};//declaration and initialization System.out.println("Three dimensional array elements are"); System.out.println(a[0][0][0]); System.out.println(a[0][0][1]); System.out.println(a[0][1][0]); System.out.println(a[0][1][1]); System.out.println(a[1][0][0]); System.out.println(a[1][0][1]); System.out.println(a[1][1][0]); System.out.println(a[1][1][1]); } } |
Output:
1 2 3 4 5 6 7 8 9 |
Three dimensional array elements are 10 20 30 40 50 60 70 80 |
Multidimensional – Using For Loop
- To insert the elements in to multi dimensional array a[][][], we are using for loop.
for i=0 to 2
for j=0 to 1
for k=0 to 1
insert the element at a[i][j][k].
2) Print the elements using for loops
for i=0 to 2
for j=0 to 1
for k=0 to 1
print a[i][j][k]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
class MultidimensionalLoop { public static void main(String args[]) { int a[][][] = new int[3][2][2]; int i, j, k, n=1; for(i=0; i<3; i++) { for(j=0; j<2; j++) { for(k=0; k<2; k++) { a[i][j][k] = n; n++; } } } System.out.println("Array elements are: \n"); for(i=0; i<3; i++) { for(j=0; j<2; j++) { for(k=0; k<2; k++) { System.out.print("a[" +i+ "][" +j+ "][" +k+ "] = " +a[i][j][k]+"\n"); } } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Array elements are: a[0][0][0] = 1 a[0][0][1] = 2 a[0][1][0] = 3 a[0][1][1] = 4 a[1][0][0] = 5 a[1][0][1] = 6 a[1][1][0] = 7 a[1][1][1] = 8 a[2][0][0] = 9 a[2][0][1] = 10 a[2][1][0] = 11 a[2][1][1] = 12 |
Using Scanner Class
- To insert the elements in to multi dimensional array array[][][] we are using scanner class.
- for i=0 to 2
- for j=0 to 1
- for k=0 to 1
- sc.nextInt() method reads the entered number and inserted the number in to array[i][j][k] .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.*; class MultiDimensionalScanner { public static void main(String args[]) { int array[][][] = new int[3][2][2]; int i, j, k; Scanner sc=new Scanner(System.in); System.out.println("Enter "+3*2*2+" Array elements: "); for(i=0; i<3; i++) { for(j=0; j<2; j++) { for(k=0; k<2; k++) { array[i][j][k] = sc.nextInt(); } } } System.out.println("Multi dimensional array elements are: \n"); for(i=0; i<3; i++) { for(j=0; j<2; j++) { for(k=0; k<2; k++) { System.out.print("array[" +i+ "][" +j+ "][" +k+ "] = " +array[i][j][k]+ "\n"); } } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Enter 12 Array elements: 1 2 3 4 5 6 7 8 9 10 11 12 Multi dimensional array elements are: array[0][0][0] = 1 array[0][0][1] = 2 array[0][1][0] = 3 array[0][1][1] = 4 array[1][0][0] = 5 array[1][0][1] = 6 array[1][1][0] = 7 array[1][1][1] = 8 array[2][0][0] = 9 array[2][0][1] = 10 array[2][1][0] = 11 array[2][1][1] = 12 |
Using String
- Initialize the strings as elements in to the multi dimensional string array str[][][].
2) Print the strings from multidimensional array str[][][].
for i=0 to 2
for j=0 to 1
for k=0 to 1
print the string available at the index str[i][j][k].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class MultiDimensionalString { public static void main(String[] args) { String[][] []str = new String[][][] {{{"one","two"}, {"three","four"}}, {{"five","six"},{"seven","eight"}}, {{"nine","ten"},{"eleven","twelve"}}}; System.out.println("Multi dimensional string array elements are :\n"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { System.out.println("str["+i+"]["+j+"]["+k+"]:"+str[i][j][k]); } } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Multi dimensional string array elements are : str[0][0][0]:one str[0][0][1]:two str[0][1][0]:three str[0][1][1]:four str[1][0][0]:five str[1][0][1]:six str[1][1][0]:seven str[1][1][1]:eight str[2][0][0]:nine str[2][0][1]:ten str[2][1][0]:eleven str[2][1][1]:twelve |