簡體   English   中英

這個二維數組中可以存儲多少個整數?

[英]How many integers can be stored in this 2-dimensional array?

我正在為考試而學習,我遇到了一個我無法解決的問題。
這里的問題是“在運行代碼后,可以在二維數組中存儲多少個不同的int值”arr“?

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

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

我認為答案是7,這就是為什么:
您創建二維數組“arr”並立即聲明它在1維中有3個開放值。
然后你說第一個開放值變成一個數組本身,包含5個開放值。
最后,for-loop表示原始數組的第二個值成為第一個值,而原始數組的第三個值則相同。 (這些值未初始化,因此它歸結為0變為0,因為這是整數的標准值)

這給了7。

雖然根據我的書,答案應該是5,但我找不到原因。

提前致謝!

它是5,因為外部數組的3個位置中的每一個都包含對同一個5元素數組的引用。 因此,在執行問題中的代碼后,如果要執行此操作:

arr[0][0] = 5;

然后以下內容將成立:

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

你說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) 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]被初始化之外,你大多是正確的。 它被設置為new int[5]

在帖子中的代碼運行之后,這就是數組的樣子:

二維數組的圖片

盡管數組的類型為int[][] ,但外部數組中的3個元素不包含int ,它們包含對int[]的引用。

內聯說明,帶注釋

/* 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]
     */
}

暫無
暫無

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

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