Given a 2d array arr in Java, the task is to print the contents of this 2d array.
Method 1: Loop method
The first thing that comes to mind is to write a nested for loop, and print each element by arr[i][j].
[ 1, 2, 3, ], [ 4, 5, 6, ], [ 7, 8, 9, ],
Method 2: Arrays.deepToString() method (Simplest method)
For this, we will use deepToString() method of Arrays class in the util package of Java. This method helps us to get the String representation of the array. This string can be easily printed with the help of print() or println() method. This is the best and simplest method to print 2D array in Java
Array: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.