java二维数组

       二维数组的定义:

  //第一种方式:
        int a[ ][ ] = {{1,2,3},{4,5,6}};


        //第二种方式;
        int[ ][ ] ints = new int[2][3];
    
        //第三种方式:第二维的长度可以动态申请
        int[ ][ ] arr3 = new int[5][ ];//五行的长度

  二维数组的遍历:

   for(int x=0; x<a.length; x++) {
            for(int y=0; y<a[x].length; y++) {
                System.out.print(a[x][y]+" ");
            }
            System.out.println();
        }
  
for(int[] cells : a) {
            for(int cell : cells) {
                System.out.print(cell+" ");
            }
             System.out.println();
        }  
  注:

  对二维复合数据类型的数组,必须首先为最高维分配引用空间,然后再顺次为低维分配空间。而且,必须为每个数组元素单独分配空间。例如:
      String s[ ][ ] = new String[2][ ];

   //为最高维分配引用空间
      s[0]= new String[2];
      s[1]= new String[2];

     //再为每个数组元素单独分配空间

   s[0][0]= new String("abc");
        s[0][1]= new String("by");
      s[1][0]= new String("sdaf");
      s[1][1]= new String("ga");

原文地址:https://www.cnblogs.com/look-up-at-the-starlit-sky/p/11504386.html