I was wondering what the simplest way of printing a 2D array? I know we can print 1D array just like this code below.
int[ ] numbers = {2, 3, 5, 7};
for (int print: numbers) {
System.out.println(print);
}
I tried to print the code below of course it didn't work.
int[ ][ ] numbers= { {1, 2, 3}, {4, 5, 6} };
for (int print: numbers) {
System.out.println(print);
}
I know I can print with nested loops like the code below but I was wondering if I can use foreach loop just like 1D array. Thank you
int[ ][ ] numbers= { {1, 2, 3}, {4, 5, 6} };
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
System.out.print(numbers[i][j] + " ");
}
System.out.println();
}
}