隨着下面的Java例子:Java多維數組 - 爲什麼只定義足夠的第一個大小?
int[] array1 = new int[]; // Incorrect, since no size is given
int[] array2 = new int[2]; // Correct
int[][][] array3 = new int[][][]; // Incorrect, since no size is given
int[][][] array4 = new int[2][2][2]; // Correct
int[][][] array5 = new int[2][][]; // Correct (why is this correct?)
所以,我的問題是,爲什麼只分配一個多維數組足以第一大小?我想你總是要指定一個尺寸,甚至多維數組的每一個人陣列的一部分,但今天我發現array5
也是一個Java的正確途徑。現在我只是想知道爲什麼。有人可以舉例說明爲什麼這適用於多維數組和/或其背後的推理嗎?
而且,我想,下面也適用,那麼:
int[][][] array6 = new int[][2][]; // Incorrect
int[][][] array7 = new int[][][2]; // Incorrect
int[][][] array8 = new int[][2][2]; // Incorrect
int[][][] array9 = new int[2][2][]; // Correct
int[][][] array10 = new int[2][][2]; // Incorrect?? (Or is this correct?)
我現在有點納悶,想澄清,如果有人知道它。
編輯/ SEMI-SOLUTION:
好吧,我找到了原因,第一部分作品:
int[][] array = new int[2][];
array[0] = new int[5];
array[1] = new int[3];
// So now I have an array with the following options within the array-index bounds:
// [0][0]; [0][1]; [0][2]; [0][3]; [0][4]; [1][0]; [1][1]; [1][2]
// It basically means I can have different sized inner arrays
唯一剩下回答是,如果:
int[][][] array10 = new int[2][][2]; // Incorrect?? (Or is this correct?)
是否有效。