In this write-up, we will explain how to use the below-listed approaches to print a 2d array in Java:
- How to use nested for-loop to print a 2d array in Java?
- How to use the for-each loop to print a 2-dimensional array?
- How to use Arrays.deepToString() to print a 2-dimensional array in Java?
So, let’s get started!
How to use nested for-loop to print a 2d array in Java?
The below-given example will guide you how to use nested for-loop to print a 2D array in java:
public static void printArray(String array[][]) {
for (int row = 0; row < array.length; row++)
{
for (int ind = 0; ind < array[row].length; ind++) {
System.out.print(array[row][ind] + " ");
}
System.out.println();
}
}
public static void main(String args[]) throws IOException {
String originalArray[][] = {
{"John", "Joe", "Mike"},
{"Shaun", "Alex", "Henry"},
{"Williams", "Dean", "Seth", "Ambrose"}};
printArray(originalArray);
}
}
Firstly, we created a printArray() method that takes a 2D array as an argument. Within the printArray() method, we utilized the nested for-loop to traverse through all the elements of the given array.
In the main method, firstly, we created a 2D string-type array and afterward we invoked the printArray() method:
The output verified that the nested for-loop successfully printed all the elements of the 2dimensional array.
How to use the for-each loop to print a 2-dimensional array?
Another way to print a 2-dimensional array is use of for-each loop. Let’s consider the following code block to learn how to print a 2D array in Java:
public static void printArray(String array[][]) {
for (String[] traverseRow : array) {
for (String ind : traverseRow) {
System.out.print(ind + ",");
}
System.out.println();
}
}
public static void main(String args[]) throws IOException {
String originalArray[][] = {
{"John", "Joe", "Mike", "Ambrose"},
{"Shaun", "Alex", "Henry"},
{"Williams", "Dean", "Seth"}};
printArray(originalArray);
}
}
This example remained the same as the previous one. The only difference is that, this time we utilized the foreach loop instead of for-loop:
This is how we can utilize the for-each loop to print a 2-dimensional array in java.
How to use Arrays.deepToString() to print a 2-dimensional array in Java?
A 2-dimensional array can be converted into a string using Java’s Arrays.deepToString() method. In the below-given code block, we’ll demonstrate how to print a 2-dimensional array in Java using the Arrays.ToString() method:
System.out.println(Arrays.deepToString(originalArray));
In this coding example, initially, we created an integer-type 2-dimensional array. After that, we printed the 2D array using the “Arrays.deepToString()” method. The detailed code and output will be shown in the following snippet:
The above snippet shows that “Arrays.deepToString()” successfully printed the elements of the 2d array.
Conclusion
Java provides multiple ways to print a 2d array, for example nested for-loop, for-each loop, Arrays.deepToString() method, etc. Each approach follows a different procedure, but all of them can still accomplish the same goal, i.e., printing a 2D array. A couple of suitable examples were provided in this post to illustrate how to print a 2d array in Java using various techniques.