2-dimensional arrays
In this code snippet:
in the second live, we specify a length for the one dimesnion, but for the second dimension, its just open and close brackets. How is it possible to do that, to be able to declare a two-dimensional array like that without specifying a length? I thought an array had to have an exact length when initialized before you could actually assign items to it.
Code :
public static int[][] pascalTriangle(int n) {
int[][] pt = new int[n][];
for (int i = 0; i < n; i++) {
pt[i] = new int[i + 1];
pt[i][0] = 1;
for (int j = 1; j < i; j++) {
pt[i][j] = pt[i - 1][j - 1] + pt[i - 1][j];
}
pt[i][i] = 1;
}
return pt;
}
in the second live, we specify a length for the one dimesnion, but for the second dimension, its just open and close brackets. How is it possible to do that, to be able to declare a two-dimensional array like that without specifying a length? I thought an array had to have an exact length when initialized before you could actually assign items to it.
0 comments:
Post a Comment