How to print a 2D array in Java?

To print a 2D array in Java, use the Arrays.deepToString function.

Printing it on one line

int[][] array = new int[][] {
  { 2, 332 },
  { 12, 77 },
  { 8, 132 },
};

// [[2, 332], [12, 77], [8, 132]]
System.out.println(Arrays.deepToString(array));

Printing subarrays on separate lines

int[][] array = new int[][] {
  { 2, 332 },
  { 12, 77 },
  { 8, 132 },
};

// [[2, 332]
// [12, 77]
// [8, 132]]
System.out.println(Arrays.deepToString(array).replace("], ", "]\n"));