package test;
import java.util.Arrays;
import java.util.Iterator;
public class arr11 {
/*
* 二维数组的定义和遍历
* 实质就是一个在堆中开辟了一个数组,然后在数组里面分成几份,每一份中再套一个数组
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] arr = new int[3][]; //本质定义一个一维数组,长度为3
arr[0] = new int[] {1,2,3};
arr[1] = new int[] {4,5,6};
arr[2] = new int[] {7,8,9};
//对数组遍历
//方式1:外层普通for循环+内层普通for循环
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr[i].length;j++) {
System.out.print(arr[i][j]+"\t");
}
}
System.out.println();
//方式2:外层增强for循环+内层增强for循环
for(int[] num:arr) {
for(int a:num) {
System.out.print(a+"\t");
}
}
System.out.println();
//方式3:外层普通for循环+内层增强for循环
for(int i=0;i<arr.length;i++) {
for(int a:arr[i]) {
System.out.print(a+"\t");
}
}
System.out.println();
//方式4:外层增强for循环+内层普通for循环
for(int[] a:arr) {
for(int i=0;i<a.length;i++) {
System.out.print(a[i]+"\t");
}
}
System.out.println();
}
}
运行: