09 May Multidimensional Arrays in Java
In the last lesson, we learned about One-Dimensional Arrays in Java and how to use them. Now, we will see the concept of arrays in Java, i.e., Multidimensional Arrays. Multi-dimensional arrays, as the name suggests, are arrays of arrays. You need to use two square brackets to declare a multidimensional array, unlike a one-dimensional array.
Video Tutorial (English)
Video Tutorial (Hindi)
Let us see an example to declare a 2 by 3 multidimensional array,
int result[][] = new int[2][3];
The following figure displays how a multidimensional array forms a 2×3 matrix,

Above, the left index determines row and the right index determines column.
We will now see how to display each element from our 2×3 matrix,
// Two-Dimensional (2D) Array in Java
class Demo {
public static void main(String[] args) {
int i, j, k = 0;
int[][] result = new int[2][3];
for(i=0; i < 2; i++) {
for(j=0; j < 3; j++) {
result[i][j] = k;
k++;
}
}
for(i=0; i < 2; i++) {
for(j=0; j < 3; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}
The following is the output displaying each element,

Let us see another example:
// Two-Dimensional (2D) Array in Java
class Demo {
public static void main(String[] args) {
int i, j, k = 0;
int[][] result = new int[3][3];
for(i=0; i < 3; i++) {
for(j=0; j < 3; j++) {
result[i][j] = k;
k++;
}
}
for(i=0; i < 3; i++) {
for(j=0; j < 3; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}
Output
0 1 2 3 4 5 6 7 8
In this lesson, we saw how to work with Multidimensional arrays in Java.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More
No Comments