这个二维数组中可以存储多少个整数?

时间:2014-01-20 18:40:47

标签: java multidimensional-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”并立即声明它在一维中有3个开放值 然后你说第一个开放值变成一个数组本身,包含5个开放值 最后,for-loop表示原始数组的第二个值成为第一个值,而原始数组的第三个值则相同。 (这些值未初始化,因此归结为0变为0,因为这是整数的标准值)

这给了7。

虽然根据我的书,答案应该是5,但我找不到原因。

提前致谢!

2 个答案:

答案 0 :(得分:6)

它是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)已初始化,否则arr[0]除了new int[5]之外,你大多数都是正确的。它被设置为int[][]

在帖子中的代码运行之后,这就是数组的样子:

Picture of 2 dimensional array

即使数组的类型为int,外部数组中的3个元素也不会保留int[],它们会保留对{{1}}的引用。

答案 1 :(得分: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]
     */
}