java二维数组的遍历
时间: 2024-01-12 09:22:46 浏览: 75
遍历二维数组JAVA代码
以下是Java二维数组的遍历的三种方法:
方法一:使用嵌套循环遍历
```java
int[][] array = new int[3][];
array[0] = new int[]{0, 1, 2};
array[2] = new int[]{4, 5, 6, 7};
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
System.out.println("null" + " ");
continue;
}
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
```
方法二:使用增强for循环遍历
```java
int[][] array = new int[3][];
array[0] = new int[]{0, 1, 2};
array[2] = new int[]{4, 5, 6, 7};
for (int[] row : array) {
if (row == null) {
System.out.println("null" + " ");
continue;
}
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
```
方法三:使用Arrays类的deepToString方法
```java
int[][] array = new int[3][];
array[0] = new int[]{0, 1, 2};
array[2] = new int[]{4, 5, 6, 7};
System.out.println(Arrays.deepToString(array));
```
阅读全文