Java二維數組遍歷輸出的方法有兩種常用的方式:
int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
輸出結果為:
1 2 3
4 5 6
7 8 9
int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int[] row : array) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
輸出結果為:
1 2 3
4 5 6
7 8 9