Simplest and Best method to print a 2D Array in Java
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]]
My Personal Notes arrow_drop_up
Please Login to comment...