二维数组的初始化和遍历
几种初始化和遍历:
public class aaaa {
public static void main(String aaa[]){
int[][] a=new int[3][];
a[0]=new int[]{1,2};
a[1]=new int[]{3,4,5,6};;
a[2]=new int[]{7,8,9,10,11};
int[][]b={
{1,1,1},{2,2,2,2},{3}
};
int[][]c=new int[3][2];
c[0]=new int[]{5,5,5,5,5};
//外层foreach,内层foreach
for (int[] k:a) {
for (int l:k) {
System.out.print(l+"\t");
}
System.out.println();
}
System.out.println();
//外层for,内层foreach
for (int i=0;i<b.length;i++){
for (int k:b[i]) {
System.out.print(k+"\t");
}
System.out.print("\n");
}
System.out.println();
//外层foreach,内层for,注意所有a[]替换为k
for (int[] k:c) {
for (int i=0;i<k.length;i++){
System.out.print(k[i]+"\t");
}
System.out.println();
}
System.out.println();
}
}