数组的浅拷贝,为什么不能简单地做newArr = oldArr? [英] Shallow copy for arrays, why can't simply do newArr = oldArr?

查看:74
本文介绍了数组的浅拷贝,为什么不能简单地做newArr = oldArr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个整数数组"orig"

Let's say I have an array of integers, "orig"

我想浅复制它,所以我不能这样做吗?

I want to shallow copy it, so can't I just do this:

int[] shallow = orig;

我的教授说,对于基元,浅拷贝和深拷贝本质上是相同的,因为我们必须在数组的每个索引上进行拷贝.但是将整个数组设置为等于另一个数组会做同样的事情,对吧?

My professor said that for primitives, shallow and deep copy are essentially the same, in that we have to copy over each index of the array. But setting the whole array equals to another array does the same thing, right?

我对对象数组也有类似的疑问

I have a similar question with object arrays

这是我的思想

Book[] objArr2 = objArr1;

但是有人告诉我,我必须复制每个数组索引,例如

But I was told that I would have to copy each array index over, like

//for loop
objArr2[i] = objArr1[i];

对于浅表复制,将相等的数组与另一个数组之间是否真的有区别,并且分别复制每个数组的索引?(我了解深度意味着您必须创建全新的对象)

For shallow copying is there really any difference between equaling arrays to another, and individually copy each array index? (I understand that deep means you have to create brand new objects)

推荐答案

我想浅复制它,所以我不能这样做吗?

I want to shallow copy it, so can't I just do this:

int []浅=原始;

int[] shallow = orig;

那并不是真正的浅表.副本是一个与原始实体相似的离散实体,但不是原始项目.在您的示例中,实际拥有的是两个指向同一对象的引用.创建副本时,应该有两个结果对象:原始对象和副本.

That's not really a shallow copy. A copy is a discrete entity that is similar to the original, but is not the original item. In your example, what you actually have are two references that are pointing to the same object. When you create a copy, you should have two resulting objects: the original and the copy.

在这里,所做的任何修改 shallow 的操作也会发生在 orig 上,因为它们都指向同一个对象.

Here, anything you do to modify shallow will happen to orig as well since they both point to the same object.

浅度"表示当您正在比较的对象具有对其中其他对象的引用时,该函数起作用.例如,如果您有一个整数数组并创建一个副本,则现在有两个包含相同整数值的数组:

"Shallowness" comes into play when the object you are comparing has references to other objects inside it. For example, if you have an array of integers and you create a copy, you now have two arrays which both contain the same integer values:

Original Array

[0]
[1]
[2]
[3]

After copying:

[0] <--- Original  [0]
[1]                [1]
[3]                [2]
[4]      Copy ---> [3]

但是,如果您有一个由对象组成的数组(假设 objArr1 objArr2 )怎么办?进行浅表复制时,您现在有两个新的数组对象,但是两个数组之间的每个对应条目都指向同一对象(因为对象本身尚未被复制;只是引用被复制了).

However, what if you had an array that consists of objects (let's say objArr1 and objArr2)? When you do a shallow copy you now have two new array objects, but each corresponding entry between the two arrays points to the same object (because the objects themselves haven't been copied; just the references have).

Original Array:

[0:]----> [object 0]
[1:]----> [object 1]
[2:]----> [object 2]
[3:]----> [object 3]

复制后(注意相应位置如何指向相同实例)

After copying (notice how the corresponding locations are pointing to the same instances):

Original -> [0:]----> [object 0] <----[:0] <- Copy
            [1:]----> [object 1] <----[:1]
            [2:]----> [object 2] <----[:2]
            [3:]----> [object 3] <----[:3]

现在,如果您通过替换条目或删除条目来修改 objArr1 ,则 objArr2 不会发生相同的事情.但是,如果您修改 objArr1 [0] 上的对象,这也会反映在 objArr2 [0] 中,因为这些位置都指向相同对象.因此,在这种情况下,即使容器对象本身是不同的,它们所包含的也是对同一对象的引用.

Now if you modify objArr1 by replacing an entry or deleting an entry, that same thing doesn't happen to objArr2. However if you modify the object at objArr1[0], that is reflected in objArr2[0] as well since those locations point to the same object. So in this case, even though the container objects themselves are distinct, what they contain are references to the same object.

进行深层复制时,将创建两个新数组,每个对应的位置都指向不同的实例.因此,从本质上讲,您会一直复制对象.

When you do a deep copy, you will two new arrays where each corresponding location points to different instances. So essentially you make copies of objects all the way down.

我的教授说,对于基元,浅拷贝和深拷贝本质上是相同的,因为我们必须在数组的每个索引上进行拷贝.

My professor said that for primitives, shallow and deep copy are essentially the same, in that we have to copy over each index of the array.

要做的重要区别是,当您复制基元数组时,您正在精确地复制值.每次您得到一个 new 原语.但是,当您有一个对象数组时,您真正 拥有的是对对象的引用数组.因此,当您创建副本时,您要做的就是创建一个 new 数组,该数组在原始数组中具有引用的副本.但是,这些引用的新副本仍指向相同的相应对象.这就是所谓的浅表副本.如果您深度复制了数组,则每个单独位置所引用的对象也将被复制.所以您会看到类似这样的内容:

The important distinction to make is that when you copy an array of primitives, you are copying the values over exactly. Each time you get a new primitive. However, when you have an array of objects, what you really have is an array of references to objects. So when you create a copy, all you have done is create a new array that has copies of the references in the original array. However, these new copies of the references still point to the same corresponding objects. This is what's known as a shallow copy. If you deep-copied the array, then the objects that each individual location refers to, will have been copied also. So you would see something like this:

Original -> [0:]----> [object 0] Copy -> [0:]----> [copy of object 0]
            [1:]----> [object 1]         [1:]----> [copy of object 1]
            [2:]----> [object 2]         [2:]----> [copy of object 2]
            [3:]----> [object 3]         [3:]----> [copy of object 3]

但是将整个数组设置为等于另一个数组会做同样的事情,对吧?

But setting the whole array equals to another array does the same thing, right?

不,不是.您在这里所做的只是简单地创建对现有数组的新引用:

No it does not. What you're doing here is simply creating a new reference to an existing array:

arr1 -> [0, 1, 2, 3, 4]

现在假设您做了 arr2 = arr1 .您拥有的是:

Now let's say you did arr2 = arr1. What you have is:

arr1 -> [0, 1, 2, 3, 4] <- arr2

因此,这里 arr1 arr2 都指向同一数组.因此,使用 arr1 执行的任何修改都将在您使用 arr2 访问阵列时反映出来,因为您正在查看的是同一阵列.制作副本时不会发生这种情况.

So here both arr1, and arr2 are pointing to the same array. So any modification you perform using arr1 will be reflected when you access the array using arr2 since you are looking at the same array. This doesn't happen when you make copies.

这篇关于数组的浅拷贝,为什么不能简单地做newArr = oldArr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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