# Multi-Dimensional Array in Java

## Multi-Dimensional Array in Java:

Multi-Dimensional Arrays represent 2D, 3D, …… arrays that are combinations of several types of arrays. For example, a 2D array is a combination of two or more 1D arrays. Similarly, a 3D array is a combination of two or more 2D arrays. In this blog, we will learn about 2D arrays.

Two-Dimensional Arrays (2D arrays):

A 2D array represents several rows and columns of data. For example, the marks obtained by a group of students in three different subjects can be represented by a 2D array. If we write the marks of three students as:

• 40, 50, 55
• 35, 69, 30
• 40, 70, 80

The preceding marks represent a two-dimensional array since it got three rows (number of students) and in each row three columns (number of subjects). There are totally (3 x 3 = 9) elements. We can take the first row itself as a one-dimensional array. Similarly, the second row is a one-dimensional array and the third row is another one-dimensional array. So the preceding 2D array contains three 1D arrays within it.

Source Code:

``````public class Two_dimensional_array {

public static void main(String[] args) {
// TODO Auto-generated method stub

int[][] students_marks = new int;

students_marks = 40;
students_marks = 50;
students_marks = 55;
students_marks = 35;
students_marks = 69;
students_marks = 30;
students_marks = 40;
students_marks = 70;
students_marks = 80;

for(int x=0; x<students_marks.length; x++) {
System.out.println();

for(int y=0; y<students_marks[x].length; y++) {
System.out.print(students_marks[x][y] + " ");
}

}

}

}``````

Another way to write the above code:

``````public class Two_dimensional_array {

public static void main(String[] args) {
// TODO Auto-generated method stub

int[][] students_marks = { {40,50,55},
{35,69,30},
{40,70,80} };

students_marks = 40;
students_marks = 50;
students_marks = 55;
students_marks = 35;
students_marks = 69;
students_marks = 30;
students_marks = 40;
students_marks = 70;
students_marks = 80;

for(int x=0; x<students_marks.length; x++) {
System.out.println();

for(int y=0; y<students_marks[x].length; y++) {
System.out.print(students_marks[x][y] + " ");
}

}

}

}``````