[英]Unable to understand this particular program
我發現很難理解此程序代碼中正在發生的事情。 請幫忙。 謝謝
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*/
}
}
它正在創建一個數組數組,像這樣
triangle[ 0 ] = new int[ 1 ];
triangle[ 1 ] = new int[ 2 ];
triangle[ 2 ] = new int[ 3 ];
...
您正在創建一個二維數組
triangle[0] = new int[1];
triangle[1] = new int[2];
triangle[2] = new int[3];
您將擁有一個二維數組,其中第0個元素的大小為1,第一個元素的大小為2,依此類推。
如果您打印的數組顯然是三角形,則這是輸出。
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("");
}
使用以上代碼片段查看打印三角形。
[[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.