2014-01-20 36 views
2

我正在爲我的考試而學習,而且我遇到了一個我無法解決的問題。
這裏的問題是「在運行代碼之後,可以在二維數組」arr「中存儲多少個不同的int值?」在這個二維數組中可以存儲多少個整數?

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

for (int i = 1; i < arr.length; i++) 
{ 
arr[i] = arr[i-1]; 
} 

我認爲答案是如圖7所示,這裏的原因:
您創建2-d陣「改編」,並立即宣佈,它在1名3個維開值。
然後你說第一個打開的值本身就是一個數組,它包含5個開放值。
最後,for-loop指出原始數組的第二個值成爲第一個值,原始數組的第三個值也是相同的。 (這些值沒有初始化,所以它歸結爲0成爲0,因爲這是一個整數的標準值),這也是7

儘管答案應該根據我的書是5

,和我找不到原因。

在此先感謝!

+0

還有就是** **參考在Java歷史名詞 - ARR [i]不拿着** **拷貝改編的[I-1]但是**參考**(指向與arr [i-1]相同的內存),所以對arr [i-1]的任何更改都會立即反映在arr [i]上,反之亦然。這比這更復雜,但基本就是這樣。 :) [幫助教程與圖像解釋參考](http://www.tutorialspoint.com/java/java_arrays.htm)。 – pasty

+1

我明白了,謝謝! – Joshua

回答

6

這是5,因爲每個外陣列的3個位置的包含到相同的5元件陣列的引用。所以執行代碼在你的問題,如果一個人要做到這一點後:

arr[0][0] = 5; 

那麼下面將舉行:如果你說the for-loop says that the second value of the original array becomes the first value and the same goes for the third value of the original array. (These values weren't initialized, so it boils down to 0 becoming 0 since that's the standard value for an integer)

arr[0][0] == 5; // true 
arr[1][0] == 5; // true 
arr[2][0] == 5; // true 

你是正確的大多隻是arr[0]被初始化。它被設置爲new int[5]

在您的文章的代碼運行在此之後是數組的樣子:

Picture of 2 dimensional array

即使數組是int[][]型,外陣列中的3個元素不持有int小號,他們持有對int[] s的引用。

+0

所以說arr [i] = arr [i-1]並沒有將[i]中實際整數的值作爲[i-1]中整數的值,而是將[i-1] (在這種情況下是一個數組),並把它放在[我]?如果是這種情況,那麼我仍然不確定您只能存儲5個值,因爲問題是「運行代碼後」。直覺上我會說,我們現在有一個5×3陣列,有15個不同的值... [編輯]剛剛看到你的編輯,先閱讀它,如果我仍然有問題,我會再次回覆。 – Joshua

+0

你有一個3x5陣列,但是5s都是一樣的。我會繪製一張照片,使其更容易形象化,給我一秒鐘,然後編輯我的帖子。 –

+0

我現在看到,我很困惑arr [0]和arr [0] [0]。還有一個問題:在運行代碼後,原始數組的第一,第二和第三位數組的引用是否保留?它不只是把所有的值都放在另一箇中,而是實際創建一個永久的引用?所以我基本上只會得到3個完全相同的數組? – Joshua

6

說明直列,帶註釋

/* Declare a two dimensional array, but only specify the first dimension 
* of '3'. This effectively leaves three arrays of undefined length 
* to which 'arr' can point. 
*/ 
int[][] arr = new int[3][]; 

/* Declare that arr[0] - the first element in the outer dimension 
* is a array of length 5. This creates 5 locations in an array of 
* length 5 into which integers can be stored. 
*/ 
arr[0] = new int[5]; 

/* No further declarations creating new space to store data in arr. 
* With no additional operations, below, to allocate memory, there 
* may never be more than 5 locations in arr to store anything. 
* This means that the rest of the question, as written, is like the 
* extra garbage you sometimes get in word problems, to try and confuse 
* you 
* 
* "A train, carrying 1000 apples, is traveling from Des Moines to Boston 
*  at 90mph. B train, carrying 1 apple, is traveling from Boston to Des Moines 
*  at 30mph on exactly teh same track. At what point on the track do they 
*  crash?" 
* 
* The apples are unnecessary to the problem at hand. 
*/ 

/* Iterate through arr - the outer array of length 3. */ 
for (int i = 1; i < arr.length; i++) 
{ 
    /* Set the current value of arr[i] to the value stored at arr[i-1]. 
    * Remember what each value of arr[] is before entering the loop... 
    * arr[0] = an array of length 5, whose values are not yet explicitly set 
    * arr[1] = null 
    * arr[2] = null 
    */ 
    arr[i] = arr[i-1]; 
    /* The first time we get this far, arr[1] will have been set to arr[0]... 
    * arr[0] = array length 5 
    * arr[1] = the same length 5 array pointed to by arr[0] 
    * arr[2] = null 
    * 
    * The second time we get this far, arr[1] will have been set to arr[0]... 
    * arr[0] = array length 5 
    * arr[1] = the same length 5 array pointed to by arr[0] 
    * arr[2] = the same length 5 array pointed to by arr[0] 
    */ 
} 
相關問題