下面是一个遍历数组中的数组的示例代码:
public class Main {
public static void main(String[] args) {
int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
// 方法一:使用嵌套循环遍历
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println("------------------------");
// 方法二:使用增强for循环遍历
for (int[] row : arr) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
}
}
输出结果为:
1 2 3
4 5 6
7 8 9
------------------------
1 2 3
4 5 6
7 8 9
这两种方法都可以遍历数组中的数组,根据实际情况选择使用哪种方法。