A 2D array in Java is an array of arrays that is commonly used to represent a matrix. To print all elements of a 2D array, we can use nested loops or built-in methods from the Arrays class.
Illustration:
Input:
mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Output: 1 2 3
4 5 6
7 8 9Input:
mat[][] = {{10, 20}, {30, 40}}
Output: 10 20
30 40
Different Ways to Print a 2D Array in Java
We can print a 2D array using the following approaches:
1. Using Nested for Loop
The simplest way to print a 2D array is by using two for loops. The outer loop traverses the rows, while the inner loop traverses the elements of each row.
class GFG {
static void print2D(int[][] mat) {
// Traverse rows
for (int i = 0; i < mat.length; i++) {
// Traverse columns
for (int j = 0; j < mat[i].length; j++) {
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] mat = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
print2D(mat);
}
}
Output
1 2 3 4 5 6 7 8 9
Explanation
- mat.length gives the number of rows.
- mat[i].length gives the number of elements in the current row.
- The outer loop moves through each row.
- The inner loop prints each element of the current row.
2. Using Nested for-each Loop
The nested for-each loop provides a simpler way to traverse the rows and elements of a 2D array without using indexes.
class GFG {
static void print2D(int[][] mat) {
for (int[] row : mat) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] mat = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
print2D(mat);
}
}
Output
1 2 3 4 5 6 7 8 9
Explanation
- The outer for-each loop gets each row.
- The inner for-each loop gets each element from the current row.
- System.out.println() moves the output to the next line after each row.
3. Using Arrays.toString()
The Arrays.toString() method can be used to convert each one-dimensional row into a readable string. We can call it for every row of the 2D array.
import java.util.Arrays;
class GFG {
public static void main(String[] args) {
int[][] mat = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int[] row : mat) {
System.out.println(Arrays.toString(row));
}
}
}
Output
[1, 2, 3] [4, 5, 6] [7, 8, 9]
Explanation
- Arrays.toString(row) converts each one-dimensional row into a string.
- Each row is printed on a separate line.
- This approach is useful when we want a readable representation of each row.
4. Using Arrays.deepToString()
The Arrays.deepToString() method can directly convert a multidimensional array into a readable string representation.
import java.util.Arrays;
class GFG {
public static void main(String[] args) {
int[][] mat = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println(Arrays.deepToString(mat));
}
}
Output
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Explanation
- Arrays.deepToString() is designed for multidimensional arrays.
- It processes nested arrays automatically.
- It is useful when we want to print the entire 2D array in a single statement.