public class test{
public static void main(String [] args){
int [][] a = new int [3][];
a[0] = new int []{1,2,3};
a[1] = new int []{4,5,6};
a[2] = new int []{7,8,9,0};
for(int temp:a)
System.out.print(temp);
}
}编译时说需要a为int型,找到的是int【】,
是我写错了,还是for each 不能用在2维数组上 呢

解决方案 »

  1.   

    public class test {
    public static void main(String[] args) {
    int[][] a = new int[3][];
    a[0] = new int[] { 1, 2, 3 };
    a[1] = new int[] { 4, 5, 6 };
    a[2] = new int[] { 7, 8, 9, 0 };
    for (int[] temp : a)
    for (int temp2 : temp)
    System.out.print(temp2 + " ");
    }
    }
      

  2.   

    int [][] a = new int [3][]; 
    a[0] = new int []{1,2,3}; 
    a[1] = new int []{4,5,6}; 
    a[2] = new int []{7,8,9,0}; 

    for (int[] is : a) {
    for (int i : is) {
    System.out.print(i);
    }
    System.out.println();
    }
    如果你用Eclipse,有个快捷方法:
    输入for的时候用联想功能,在联想列表中选择foreach,敲回车
    然后在这个foreach循环里面再次重复上面的动作,这样两个foreach循环就出来了