简体   繁体   English

无法理解此特定程序

[英]Unable to understand this particular program

I'm finding it difficult to understand what is happening in this program code. 我发现很难理解此程序代码中正在发生的事情。 Please help. 请帮忙。 Thanks 谢谢

public class ArrayTriangle
{ 
    public static void main( String[] args )
    {
        int[][] triangle = new int[10][];
        for ( int j = 0; j<triangle.length ; j++ )

            triangle[ j ] = new int[ j + 1 ];         /*Please explain me this line*/
    }
}   

It is creating an array of array, like this 它正在创建一个数组数组,像这样

triangle[ 0 ] = new int[ 1 ];
triangle[ 1 ] = new int[ 2 ];
triangle[ 2 ] = new int[ 3 ];
...

You are creating a 2-D array 您正在创建一个二维数组

triangle[0] = new int[1];
triangle[1] = new int[2];
triangle[2] = new int[3];

You can will will have a 2-D array with 0th element having size of 1, 1st with 2 and so on. 您将拥有一个二维数组,其中第0个元素的大小为1,第一个元素的大小为2,依此类推。

This is the output if you print the array which is clearly a triangle. 如果您打印的数组显然是三角形,则这是输出。

for (int j = 0; j < triangle.length; j++){
        for(int k = 0; k < triangle[j].length ; k++){
            System.out.print(triangle[j][k]);
        }

        System.out.println("");
    }

Use the above snippet to see print your triangle. 使用以上代码片段查看打印三角形。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM