【问题标题】:How many integers can be stored in this 2-dimensional array?这个二维数组可以存储多少个整数?
【发布时间】:2014-01-20 18:40:46
【问题描述】:

我正在准备考试,但遇到了一个我无法解决的问题。
这里的问题是“运行代码后,二维数组“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,原因如下:
您创建二维数组“arr”并立即声明它在一维中具有 3 个开放值。
然后你说第一个开放值变成了一个数组本身,包含 5 个开放值。
最后,for循环说原始数组的第二个值成为第一个值,原始数组的第三个值也是如此。 (这些值未初始化,因此归结为 0 变为 0,因为这是整数的标准值)

给出 7。

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

提前致谢!

【问题讨论】:

  • java 中有 reference 的历史术语 - arr[i] 不持有 arr[i-1] 的 copy 而是reference(指向与 arr[i-1] 相同的内存)到数组,因此对 arr[i-1] 的任何更改都会立即反映在 arr[i] 上,反之亦然。它比这更复杂,但基本上就是这样。 :) Helper tutorial with image explaining reference.
  • 我现在明白了,谢谢!

标签: java multidimensional-array


【解决方案1】:

它是 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[][],外部数组中的 3 个元素不包含 ints,它们包含对 int[]s 的引用。

【讨论】:

  • 所以说 arr[i] = arr[i-1] 不会使 [i] 中的实际整数值成为 [i-1] 中整数的值,而是复制任何内容在 [i-1] 中(在这种情况下是一个数组)并将其放在 [i] 中?如果是这种情况,那么我仍然不确定如何只能存储 5 个值,因为问题是“运行代码后”。直观地说,我们现在有一个 5x3 数组,有 15 个不同的值...Edit 刚刚看到您的编辑,先阅读它,如果我还有问题,我会再次回复。
  • 您有一个 3x5 数组,但 5s 都是一样的。我会画一张更容易可视化的图片,给我一点时间,我会编辑我的帖子。
  • 我现在明白了,我混淆了 arr[0] 和 arr[0][0]。还有一个问题:在运行代码后,原始数组的第一个、第二个和第三个位置对数组的引用是否保留?它不仅从一个中获取所有值并将其放入另一个中,而且实际上创建了一个永久引用?所以我基本上只会得到 3 个完全相同的数组?
  • 好的,编辑添加图片和更多解释。
  • 那种。实际上,您将有 3 种不同的方式来访问同一个 5 元素数组。
【解决方案2】:

内联解释,带有 cmets

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

【讨论】:

    猜你喜欢
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    相关资源
    最近更新 更多