我如何使用 for 循环编写更短的代码,这可能吗?而不是写作;
int array[][] = new int[10][];
array[0] = new int[1];
array[1] = new int[2];
array[2] = new int[3];
array[3] = new int[4];
array[4] = new int[5];
array[5] = new int[6];
array[6] = new int[7];
array[7] = new int[8];
array[8] = new int[9];
array[9] = new int[10];
这是我的代码;
int array[][] = new int[10][];
array[0] = new int[1];
array[1] = new int[2];
array[2] = new int[3];
array[3] = new int[4];
array[4] = new int[5];
array[5] = new int[6];
array[6] = new int[7];
array[7] = new int[8];
array[8] = new int[9];
array[9] = new int[10];
for (int i = 0; i < 10; i++){
for (int j = 0; j < i + 1; j++) {
array[i][j] = i + j;
}
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < i + 1; j++)
System.out.printf("%-4d",array[i][j]);
System.out.println();
}
}
}