【问题标题】:Java: Declaring a multidimensional array without specifying the size of the array ( eg. new int[10][] )Java:声明一个多维数组而不指定数组的大小(例如 new int[10][] )
【发布时间】:2012-04-11 10:09:05
【问题描述】:

我一直试图弄清楚这里到底发生了什么。我只是想弄清楚我在下面评论过的 2 行在做什么。我发现这个程序没有声明数组的完整维度(而不是 new int[10][5]; 它只是决定不通过说“new int[10][];”来声明它,就像第二个数组长度无关紧要(将其更改为 1 或 100 不会影响输出)。

int[][] tri = new int[10][];  //this lack of giving the size of the 2nd array is strange
 for (int r=0; r<tri.length; r++) {
 tri[r] = new int[r+1];   //I'm not sure what this line is doing really 
}
for (int r=0; r<tri.length; r++) {
 for (int a=0; a<tri[r].length; a++) {
     System.out.print(tri[r][a]);  
     }
 System.out.println();
 }

【问题讨论】:

    标签: java arrays multidimensional-array


    【解决方案1】:

    在Java中最好使用多维集合。

    语法:

    ArrayList &lt;Object&gt; x= new ArrayList &lt;Object&gt;();

    下面是Java中多维集合的一个应用。

    
    import java.util.*; 
    
    class MultidimensionalArrayList { 
    
        /*function for creating and returning 2D ArrayList*/
        static List create2DArrayList() 
        { 
            /*Declaring 2D ArrayList*/
            ArrayList<ArrayList<Integer> > x 
                = new ArrayList<ArrayList<Integer> >(); 
    
            /*one space allocated for 0th row*/
            x.add(new ArrayList<Integer>()); 
    
            /*Adding 3 to 0th row created above x(0, 0)*/
            x.get(0).add(0, 3); 
    
            /*Creating 1st row and adding values 
        (another way for adding values in 2D collections)*/
            x.add(new ArrayList<Integer>(Arrays.asList(3, 4, 6))); 
    
            /*Add 366 to 1st row 0th column x(1, 0)*/
            x.get(1).add(0, 366); 
    
            /*Add 576 to 1st row 4th column x(1, 4)*/
            x.get(1).add(4, 576); 
    
            /*Adding values to 2nd row*/
            x.add(2, new ArrayList<>(Arrays.asList(3, 84))); 
    
            /*Adding values to 3rd row*/
            x.add(new ArrayList<Integer>(Arrays.asList(83, 6684, 776))); 
    
            /*Adding values to 4th row*/
            x.add(new ArrayList<>(Arrays.asList(8))); 
    
            return x; 
        } 
    
        public static void main(String args[]) 
        { 
            System.out.println("2D ArrayList :"); 
    
            /*Printing 2D ArrayList*/
            System.out.println(create2DArrayList()); 
        } 
    } 
    
    
    

    【讨论】:

      【解决方案2】:

      如果您将多维数组视为数组的数组,则更有意义:

      int [][] tri = new int[10][]; // This is an array of 10 arrays
      
      tri[r] = new int[r+1]; // This is setting the r'th array to
                             // a new array of r+1 ints
      

      【讨论】:

        【解决方案3】:

        所有new int[10][] 都在声明一个大小为10 的数组,其中包含null 数组。

        for 循环中,null 数组被实例化为不断增加的数组大小。

        【讨论】:

          【解决方案4】:

          不缺,基本没有设置具体的数量,不是必须的,因为可以有很多字段

          第二行

          tri[r] = new int[r+1];
          

          正在将所有字段设置为不为空

          【讨论】:

            【解决方案5】:

            它只是声明一个包含 10 个数组的数组。每个“子”数组的长度都可以不同。

            【讨论】:

              【解决方案6】:

              第一行是一个 int 数组。为 int 数组创建了 10 个槽。

              第三行创建一个新的 int 数组并将其放入您最初创建的插槽之一。新的 int 数组中有 r+1 个用于 int 的空格。

              因此,位置 0 的 int 数组将有 1 个用于 int 的插槽。位置 1 的 int 数组将有 2 个用于 int 的插槽。整体形状将是:

              [
                  [0],
                  [0,0],
                  [0,0,0],
                  ...,
                  [0,0,0,0,0,0,0,0,0,0]
              ]
              

              用变量名tri暗示(它看起来像一个三角形)

              【讨论】:

              • [对不起,我的上一条评论不完整,我不能删除它]所以看起来第一个数组长度必须始终声明,但之后数组长度的长度可以在点? (例如 new int[10][]; 可以,因为它声明了第一个长度)。还有'tri[r] = new int[r+1];'这一行似乎创建了数组的第二部分,但是是否可以使这个数组比第一个更大(当我这样做时会创建异常)?
              • 您总共有 11 个数组。第一个数组是其他 10 个数组的数组。 new int[10][] 说“让我成为一个可以容纳int[] 对象的数组,并为它们创建 10 个插槽。” sn-p 的第三行说“用r+1 插槽创建一个整数数组,并将新数组放入插槽r 的现有数组中。”您可以根据需要制作 11 个阵列中的任何一个。所有 11 种尺寸都是独立的。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2021-07-13
              • 1970-01-01
              • 1970-01-01
              • 2012-08-13
              • 2020-04-24
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多