复制数组列表的构造函数 [英] Copy constructor of Array List
问题描述
我刚刚阅读了人们认为的ArrayList的深层副本
I just read about deep copy of ArrayList, that people think
new ArrayList<>(originalList);
将创建一个浅表副本.我写了一个小演示
will create a shallow copy. And I wrote a small demo
ArrayList<String> originalNameList = new ArrayList<>(Arrays.asList("Anna", "Betty", "Chris"));
List<String> copyNameList1=originalNameList;
List<String> copyNameList2= new ArrayList<>(originalNameList);
originalNameList.add("Duke");
copyNameList1.add("Ellen");
copyNameList1.set(1,"Bill");
copyNameList2.set(0,"Andy");
System.out.println("originalNameList = " + originalNameList);
System.out.println("copyNameList1 = " + copyNameList1);
System.out.println("copyNameList2 = " + copyNameList2);
结果:
originNameList = [Anna, Bill, Chris, Duke, Ellen]
copyNameList1 = [Anna, Bill, Chris, Duke, Ellen]
copyNameList2 = [Andy, Betty, Chris]
我觉得ArrayList的复制构造函数并不那么浅.那为什么人们这么说呢?是否有一些深层复制? 非常感谢!
I feel that copy constructor of ArrayList is not that shallow. That why people say that so? Are there some levels of deep copy? Thank a lot!
推荐答案
您对浅"和深"副本的定义似乎是错误的.似乎您认为浅表副本的意思是复制对对象的引用",如下所示:
Your definitions for "shallow" and "deep" copy seem to be wrong. It seems like you think a shallow copy means "copying the reference to the object", like this:
List<String> copyNameList1=originalNameList;
深层"副本将创建一个新的数组列表:
Whereas a "deep" copy creates a new array list:
List<String> copyNameList2= new ArrayList<>(originalNameList);
请注意,以上是一个误解.
Note that the above is a misconception.
浅拷贝实际上是指使用相同的元素创建一个新的数组列表,而深拷贝则是创建一个具有原始数组列表中的元素副本的新数组列表.
A shallow copy actually means creating a new array list with the same elements, while a deep copy means creating a new array list that has copies of the elements in the original array list.
这里最大的区别是是否复制了数组列表中的元素".
The big difference here is "whether the elements in the array list are copied".
想象一下,您有一个数组列表对象a1
.它包含对3个三个对象obj1
,obj2
,obj3
的引用.
Imagine you have an array list object a1
. It contains references to 3 three objects obj1
, obj2
, obj3
.
a1
的浅表副本将创建一个新的数组列表对象a2
,其中包含对相同的obj1
,obj2
,obj3
对象的引用.
A shallow copy of a1
will create a new array list object a2
that contains references to the same obj1
, obj2
, obj3
objects.
a1
的深层副本将创建一个新的数组列表对象a2
,其中包含对obj4
,obj5
,obj6
的引用,其中obj4
,obj5
和obj6
是副本分别是obj1
,obj2
和obj3
的
A deep copy of a1
will create a new array list object a2
that contains references to obj4
, obj5
, obj6
, where obj4
, obj5
and obj6
are copies of obj1
, obj2
, and obj3
respectively.
以下两种方法可以显示浅拷贝和深拷贝中发生的情况:
Here are two methods that shows what happens in shallow copy and deep copy:
static ArrayList<String> shallowCopy(ArrayList<String> original) {
ArrayList<String> copy = new ArrayList<>();
for (String s : original) {
copy.add(s);
}
return copy;
}
static ArrayList<String> deepCopy(ArrayList<String> original) {
ArrayList<String> copy = new ArrayList<>();
for (String s : original) {
String copyOfString = new String(s); // note that each string is copied as well
copy.add(copyOfString);
}
return copy;
}
通常,深层副本也会复制目标对象所引用的对象.
In general, a deep copy copies the objects that the target object refers to as well.
这篇关于复制数组列表的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!