how to print 2d array in java code example
Example 1: how to print a 2d array in java
for (int row = 0; row < arr.length; row++)
{
for (int col = 0; col < arr[row].length; col++)
{
System.out.printf("%5d", arr[row][col]);
}
System.out.println();
}
Example 2: print 2d array in java
int[][] array = new int[rows][columns];
System.out.println(Arrays.deepToString(array));
Example 3: java print 2d array as table
static void debugV2(Object... obj) {
System.out.println(Arrays.deepToString(obj)
.replace("],", "\n").replace(",", "\t")
.replaceAll("[\\[\\]]", " "));
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj).replace("], ", "]\n"));
}