Mastering Multidimensional Arrays in Java: A Beginner’s Guide
In Java, you can define an array of multi-dimensional arrays. Before learning about multi-dimensional arrays.
In that tutorial, you learned how to create and use an array of primary data types (such as Double, int, etc.), a string array, and an object array. An array of arrays can also be created. For example,
int [] [] a = new int [3] [4];
A is a two-dimensional array that holds up to 12 integer elements.
Note that the array starts from 0, not 1.
You can also define a three-dimensional array. For example:
String [] [] [] personalInfo = new String [3] [4] [2];
PersonalInfo is a 3D array that holds up to 24 string elements.
In Java, the components of a multi-dimensional array are also arrays.
If you are familiar with C++ / C, you may feel that multi-dimensional arrays work similarly in Java and C++ / C. Well, it is not. In Java, rows can have different lengths.
You will notice the difference when initializing.
How do you initialize a 2D array in Java?
Here is an example of how to initialize a 2D array in Java.
int [] [] a = { {1, 2, 3}, {4, 5, 6, 9}, {7}, };
As mentioned, each array component a is an array in itself, and the length of each row is different.
Let’s write a program to prove it.
- class MultidimensionalArray {
- public static void main (String [] args) {
- int [] [] a = {
- {1, 2, 3},
- {4, 5, 6, 9},
- {7},
- };
- System.out.println (“Length of row 1:” + a [0] .length);
- System.out.println (“Length of row 2:” + a [1] .length);
- System.out.println (“Length of row 3:” + a [2] .length);
- }
- }
Output
Length of row 1: 3
Length of row 2: 4
Length of row 3: 1
Since each component of a multi-dimensional array is also an array ([a [0], a [1, and [a [2 are also arrays), You can use the length property to find the length of each row.
Example: Print all the elements of a 2D array using a loop
- class MultidimensionalArray {
- public static void main (String [] args) {
- int [] [] a = {
- {1, -2, 3},
- {-4, -5, 6, 9},
- {7},
- };
- for (int i = 0; i <a.length; ++ i) {
- for (int j = 0; j <a [i] .length; ++ j) {
- System.out.println (a [i] [j]);
- }
- }
- }
- }
Output
It may be best to use the for… each loop at all times. You can do the same with the for… each loop:
- class MultidimensionalArray {
- public static void main (String [] args) {
- int [] [] a = {
- {1, -2, 3},
- {-4, -5, 6, 9},
- {7},
- };
- for (int [] innerArray: a))
- for (int data: innerArray) {
- System.out.println (data);
- }
- }
- }
- }
Output
1
-2
3
-4
-5
6
9
7
How do you initialize a 3D array in Java?
You can set the 3D array in the same way as a 2D array. Consider the following example:
// test is a 3d array int [] [] [] test = = { {1, -2, 3}, {2, 3, 4} }, { {-4, -5, 6, 9}, {1}, {2, 3} } };
In essence, a three-dimensional array is an array of two-dimensional arrays.
Similar to 2D arrays, rows of 3D arrays can have different lengths.
Example: A program for printing 3D array elements using loops
- class ThreeArray {
- public static void main (String [] args) {
- // test is a 3d array
- int [] [] [] test = =
- {
- {1, -2, 3},
- {2, 3, 4}
- },
- {
- {-4, -5, 6, 9},
- {1},
- {2, 3}
- }
- };
- // for..each loop to iterate through elements of 3d array
- for (int [] [] array2D: test) {
- for (int [] array1D: array2D) {
- for (int item: array1D) {
- System.out.println (item);
- }
- }
- }
- }
- }
Output
1
-2
3
2
3
4
-4
-5
6
9
1
2
3
Loop Through a Multi-Dimensional Array
You can also use a for loop
inside another for loop
to get the elements of a two-dimensional array (we still have to point to the two indexes):
Example
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
for (int i = 0; i < myNumbers.length; ++i) {
for (int j = 0; j < myNumbers[i].length; ++j) {
System.out.println(myNumbers[i][j]);
}
}
FAQ
What is a multidimensional array in Java?
It's an array of arrays, allowing you to store data in a table-like format with rows and columns.
How do I declare a 2D array in Java?
Use syntax like int[][] arrayName; to declare a 2D array.
Can Java arrays have rows of different lengths?
Yes, Java supports jagged arrays where each row can have a different number of columns.