簡體   English   中英

Java多維數組-為什么僅定義第一個大小就足夠了?

[英]Java multidimensional array - Why is only defining the first size enough?

帶有以下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?)

我現在有些困惑,如果有人知道,我想澄清一下。


編輯/半解決方案:

好的,我發現了為什么第一部分起作用:

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?)

是否有效。

從簡單開始:二維數組是數組的數組。 Java中的數組是對象。 因此,僅定義第一個大小會創建給定大小的數組,該數組可以存儲其他數組,但此時這些數組仍為空。 因此,在使用它之前,您需要調用array1[0] = new int[5] ,否則會得到NullPointerException。 對於更高維的數組,這適用。

從理論上講,所有“內部”數組實際上都可以具有不同的長度。 因此,您可以編寫如下內容: array1[0] = new int[1]; array1[1] = new int[4]; array1[0] = new int[1]; array1[1] = new int[4];

關於您的最后一個問題:這是無效的,Java編譯器會說類似“在空維之后不能指定數組維”的內容。 這是由於以下事實:未指定第二級,因此是第一級數組中的空對象,因此,不能在這些空數組上指定維。

因為您沒有定義多維數組。 您實際上定義的是int數組的數組。 例如,您可以執行以下操作:

int[][] array1 = new int[5][];
for(int i = 0; i < 5; i++){
    array1[i] = new int[i];
}

這導致鋸齒狀數組。

因此, int[2][]定義了一個數組數組。 int[2][2]定義了一個數組數組,並且還定義了所有內部數組。 int[][2]試圖定義所有內部數組,而沒有放入它們的任何內容,因此失敗。

另一種思考的方式是,您以后可以更改存儲在最外層數組中的引用(即更改一行值),但是不能沿另一條軸進行修改(更改列)。 所以這是有效的:

int[][] arr = new int[2][2];
arr[0] = new int[3];

雖然不是:

int[][] arr = new int[2][2];
arr[][0] = new int[3];

這是因為

int[][][] array = new int[n][m][p];

等效於創建n維數組,然后用 m維數組的n個實例的引用填充它,然后用 m維數組的m個實例的引用填充該數組。

如果並非所有維都存在,則說明您已部分初始化了數組。

int[][][] array5 = new int[2][][]; // Correct (why is this correct)

編寫此代碼可以實例化一個int [] []數組。 在內存中,它是對int [] []的引用的數組。 您不需要知道int [] []的大小,因為它只是一個參考,並且每個大小可能都不同。

以這種方式查看內存:

array5:[length a ref, length of a ref]

然后實例化subArray0:

... ,subArray0[ length of a ref, length of a ref], ...

並影響子數組到主數組

array5:[refToSubArray0, length of a ref],

但是無論子數組的長度如何,您都只需將引用的長度保存在內存中即可將子數組存儲在其中,因為它存儲在其他位置。

int[][][] array10 = new int[2][][2]; // Correct?? (Is this correct?)

這是不正確的。 我相信當你這樣做

new int[2][2][2]

您在內存中分配:

array[ref0,ref1], sub0[ref00,ref01], sub1[ref10,ref11], sub00[int000, int001], sub00[int000, int001], sub01[int010, int011], sub10[int100, int101], sub11[int110, int111] ...

如果跳過數組大小,則它無法為第三個子數組分配內存,因為它不知道將實例化多少個子數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM