Why do multidimensional arrays behave opposite from what you would expect when creating them? An int[][]
array means that I should have an array of arrays of ints. I would read it backwards starting from the rightmost brackets: ((int)[])[]
.
And I would expect the following code,
int[][] grid = new int[][3];
// ...Proceed to fill nested arrays
to create a 3-element array of int arrays. But it produces a compiler error. However this,
int[][] grid = new int[3][];
does not. The first example makes sense to me, because it seems like I am creating a 3-element array of null int array references. But the second example doesn't make sense to me, since it seems as though the actual array of int arrays has no set size, but the 'nested' arrays are still initialized with a size of 3. Why is this?