通过Arrays.copyOf()生成的二维数组副本中的更改反映在原始数组中 [英] Changes in the copy of a 2-D array produced via Arrays.copyOf() reflect in the the original array

查看:57
本文介绍了通过Arrays.copyOf()生成的二维数组副本中的更改反映在原始数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我用Java创建2D int 数组,然后使用 Arrays.copyOf()制作一个副本,就像这样-

If I create a 2D int array in Java, and then make a copy of it using Arrays.copyOf(), like so -

jshell> int[][] c1 = {{1,2}, {3,4}}
c1 ==> int[2][] { int[2] { 1, 2 }, int[2] { 3, 4 } }

jshell> int[][] d1 = Arrays.copyOf(c1, c1.length)
d1 ==> int[2][] { int[2] { 1, 2 }, int[2] { 3, 4 } }

如果我然后更改副本中的元素,为什么原始2D数组中的对应单元在此过程中发生突变?

If I then change an element in the copy, why does the corresponding cell in the original 2D array get mutated in the process?

jshell> d1[0][0] = 0
$21 ==> 0

jshell> d1
d1 ==> int[2][] { int[2] { 0, 2 }, int[2] { 3, 4 } }

jshell> c1
c1 ==> int[2][] { int[2] { 0, 2 }, int[2] { 3, 4 } } // c1[0][0] was 1 originally

这使我相信,在使用 Arrays.copyOf()复制2D数组的过程中,仅为最外面的数组创建一个单独的副本,而每个内部数组仍是对该数组的引用.原始二维数组的内部数组?

This leads me to believe that during a copy of 2D arrays using Arrays.copyOf(), a separate copy is created only for the outermost array, while each inner array is still a reference to the inner arrays of the original 2D array?

jshell> d1 = null
d1 ==> null

jshell> c1
c1 ==> int[2][] { int[2] { 0, 2 }, int[2] { 3, 4 } }

如果是这样,为什么会这样呢?至少每个

If so, why is it this way? Shouldn't Arrays.copyOf() create distinct copies, at least per the docs? Is this behavior documented anywhere in the Oracle docs?

最后,创建2D数组的不同副本的正确方法是什么,与 Arrays.copyOf()用于1D数组的方法相同?

Lastly, what is the correct way to create distinct copies of a 2D array, the same way Arrays.copyOf() works for 1D arrays?

推荐答案

2D数组基本上是包含数组的数组,而 Arrays.copyOf 进行浅表复制,因此仅外部数组(复制数组数组),而不是复制数组内部值(在这种情况下,是 int 的数组,即 int [] ).结果,原件和副本都包含相同的 int [] 数组,因此,如果您通过一个进行修改,那么通过另一个也可以看到结果.

A 2D array is basically an array that contains arrays, and Arrays.copyOf does a shallow copy, so only the outer array (the array of arrays) is copied, not the values inside the array (in this case, arrays of int, ie int[]). As a result, both the original and the copy contain the same int[] arrays, so if you modify through one, the result is also visible through the other.

javadoc中明确提到了这一点:

This is explicitly mentioned in the javadoc:

对于在原始数组和复制后,两个数组将包含相同的值.

For all indices that are valid in both the original array and the copy, the two arrays will contain identical values.

您确实需要在了解签名的情况下进行阅读:< T>T [] copyOf(T [],int).复制的数组是 T [] ( T 的数组,其中 T int [] ) T [] []

You do need to read that with the knowledge of the signature: <T> T[] copyOf(T[], int). The array copied is T[] (an array of T, where T is int[]), not T[][]!

对于2D数组的深层副本,您将必须自己对数组进行深层副本,例如,请参见

For a deep copy of a 2D array, you will have to deep copy the array yourself, for example see How do I do a deep copy of a 2d array in Java?

这篇关于通过Arrays.copyOf()生成的二维数组副本中的更改反映在原始数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆