Java的:(如新的INT [10] [])声明一个多维数组不指定数组的大小 [英] Java: Declaring a multidimensional array without specifying the size of the array ( eg. new int[10][] )

查看:160
本文介绍了Java的:(如新的INT [10] [])声明一个多维数组不指定数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出究竟发生在这里。我只是想弄清楚的2号线正在做的事情,我已经评论如下。我发现这个程序,不声明数组的全尺寸(而不是新的INT [10] [5],它只是决定不应该说宣布它新的INT [10] [];这就像第二数组长度并不重要(它改变为1或100不影响输出)。

  INT [] [] =三新INT [10] []; //这种缺乏给予第二数组的大小是奇怪
 对于(INT R = 0;为r tri.length; R ++){
 三[R] =新的INT [R + 1]; //我不知道这行做真
}
对于(INT R = 0;为r tri.length; R ++){
 对于(int类型的= 0; A<三[R]。长度; A ++){
     System.out.print(三[R] [A]);
     }
 的System.out.println();
 }


解决方案

第一行使得INT数组的数组。有10个插槽,用于创建INT数组。

第三行创建了一个新的int数组,并把它放在你首先做一个插槽。新的int数组有R + 1位为整数它。

所以,在位置0的int数组将有一个int 1插槽。在位置1的int数组将有2个插槽,一个int。整体的形状的将是:

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

这是在与变量名三暗示

(它看起来像一个三角形)

I've been trying to figure out what exactly is happening here. I'm just trying to figure out what the 2 lines are doing that I've commented on below. I found this program that doesn't declare the full dimensions of the array (instead of new int[10][5]; it just decides to not declare it by saying 'new int[10][];' It's like the 2nd array length doesn't matter (changing it to 1 or 100 doesn't affect the output).

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();
 }

解决方案

The first line makes an array of int arrays. There are 10 slots for int arrays created.

The third line creates a new int array and puts it in one of the slots you made at first. The new int array has r+1 spaces for ints in it.

So, the int array in position 0 will have 1 slot for an int. The int array in position 1 will have 2 slots for an int. The overall shape will be:

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

which is hinted at with the variable name tri (it looks like a triangle)

这篇关于Java的:(如新的INT [10] [])声明一个多维数组不指定数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆