Java 为什么在Arraylist副本中进行修改会修改原件?
我正在尝试将ArrayList的内容复制到另一个ArrayList,并更改copy的内容。我不想在原稿中反映这一点 我查了一下,做了相应的修改,还是一样的问题。有人能帮忙吗?我正在分享以下代码:Java 为什么在Arraylist副本中进行修改会修改原件?,java,arraylist,cloneable,Java,Arraylist,Cloneable,我正在尝试将ArrayList的内容复制到另一个ArrayList,并更改copy的内容。我不想在原稿中反映这一点 我查了一下,做了相应的修改,还是一样的问题。有人能帮忙吗?我正在分享以下代码: private ArrayList<CustVoiceListObject> FilterApprovedWorkFromList() { ArrayList<CustVoiceListObject> arrayListCopy = new ArrayList&
private ArrayList<CustVoiceListObject> FilterApprovedWorkFromList() {
ArrayList<CustVoiceListObject> arrayListCopy = new ArrayList<>(arrayListCustVoice);
for(int i =0; i<arrayListCopy.get(position).getPackageArray().size();i++)
{
if(!arrayListCopy.get(position).getPackageArray().get(i).getPackageApproved().equals("Y"))
{
arrayListCopy.get(position).getPackageArray().remove(i);
i--;
}
}
return arrayListCopy;
}
事实上,我已经在我原来的类中实现了Cloneable,但我仍然面临同样的问题
更新2[研究结论]
我遇到了这个
在我的例子中,有两个类。第二个是第一个的子集。粘贴如下:
public class CustVoiceListObject implements Cloneable {
private String txtSource, txtCustComment, txtCustOk, txtRepeat;
private int numberOfPackages, complaintSerial;
private ArrayList<CustomerVoicePackageListObject> packageArray;
private Double totalAmount;
//getters & setters & constructors
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
.clone()
必须链接到类的clone()
,而不是其他地方。如果正确的话,它将促使我采取措施,根据我在每个单独的类中实现clone()来解决异常
这就是我所做的,将for循环修改为:
private CustVoiceListObject FilterApprovedWorkFromList() {
//Observe the change here. It's no more ArrayList, it's Class type
CustVoiceListObject arrayListCopy = null;
try {
arrayListCopy = (CustVoiceListObject) arrayListCustVoice.get(position).clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
for(int i =0; i<arrayListCopy.getPackageArray().size();i++)
{
if(!arrayListCopy.getPackageArray().get(i).getPackageApproved().equals("Y"))
{
arrayListCopy.getPackageArray().remove(i); //this is ArrayList<ClassType>. Nested class objects.
arrayListCopy.setTxtCustOk("OKOK"); //within the class
i--;
}
}
return arrayListCopy;
}
private CustVoiceListObject过滤器ApprovedWorkFromList(){
//观察这里的变化。它不再是ArrayList,而是类类型
CustVoiceListObject arrayListCopy=null;
试一试{
arrayListCopy=(CustVoiceListObject)arrayListCustVoice.get(position.clone();
}捕获(CloneNotSupportedException e){
e、 printStackTrace();
}
对于(int i=0;i而言,问题在于原始ArrayList
的元素是引用值,因此您只需将引用复制到这些对象,而不是对象本身(它们似乎是另一种数组)
看看本质上处理同一类问题的问题。问题是原始ArrayList
中的元素是引用值,所以您只需复制对这些对象的引用,而不是对象本身(似乎是另一种数组)
看看本质上处理同一类问题的问题。复制操作复制了原始列表,但没有复制列表的元素
也就是说,如果oldList
具有以下Person
对象:
oldList
:John
,Jane
,Jude
,Joe
您将oldList
复制到newList
:
oldList
:John
,Jane
,Jude
,Joe
newList
:John
,Jane
,Jude
,Joe
然后从newList
中删除John
:
oldList
:John
,Jane
,Jude
,Joe
newList
:Jane
,Jude
,Joe
您可以看到它们是两个不同的列表。但是您不是在更改列表,而是在更改列表中的对象。如果Joe
的名字更改为Jim
,您将:
oldList
:John
,Jane
,Jude
,Jim
newList
:Jane
,Jude
,Jim
您只完成了所谓的“浅”复制。您希望执行“深”复制,如图所示
编辑(到有问题的“更新2”)
恐怕你的研究还不够深入。在你的(新)一班:
复制操作复制了原始列表,但没有复制列表的元素
也就是说,如果oldList
具有以下Person
对象:
oldList
:John
,Jane
,Jude
,Joe
您将oldList
复制到newList
:
oldList
:John
,Jane
,Jude
,Joe
newList
:John
,Jane
,Jude
,Joe
然后从newList
中删除John
:
oldList
:John
,Jane
,Jude
,Joe
newList
:Jane
,Jude
,Joe
您可以看到它们是两个不同的列表。但是您不是在更改列表,而是在更改列表中的对象。如果Joe
的名字更改为Jim
,您将:
oldList
:John
,Jane
,Jude
,Jim
newList
:Jane
,Jude
,Jim
您只完成了所谓的“浅”复制。您希望执行“深”复制,如图所示
编辑(到有问题的“更新2”)
恐怕你的研究还不够深入。在你的(新)一班:
替换这一行并检查AARYLISABAREListReopy=新的AARYListCudio(RayayListCuthStVoice);通过ArayLeLayayListCopy:ArayListCuthStVoice;这是我的第一次尝试。失败。我不认为这个问题的分类是重复的,APP。这些解决方案中没有一个是有效的。替换这一行并检查ARARYLISABARYListPosie=新数组列表。(arrayListCustVoice)我不认为这个问题的分类是重复的,没有一个解决方案是这样的。我总结了同样的结论。你有没有对java的引用?我用java的一个不同的链接更新了我的答案——虽然这确实需要你的类实现。nts可克隆接口请检查我添加的更新,然后让know@Krsna我已经编辑了我的答案以回应您的更新谢谢,我会尽力让您知道,因为这是一件需要理解的重要事情,尽管我暂时删除了这个概念,以便在最后期限之前完成。我得出了相同的结论。您有参考资料吗ce for Java?我用一个不同的Java链接更新了我的答案
public class CustomerVoicePackageListObject implements Cloneable {
public String packageCategory;
public String packageName;
public String partUsageFlag;
public String laborUsageFlag;
public String status;
public String isApproved;
//getters & setters & constructors
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
private CustVoiceListObject FilterApprovedWorkFromList() {
//Observe the change here. It's no more ArrayList, it's Class type
CustVoiceListObject arrayListCopy = null;
try {
arrayListCopy = (CustVoiceListObject) arrayListCustVoice.get(position).clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
for(int i =0; i<arrayListCopy.getPackageArray().size();i++)
{
if(!arrayListCopy.getPackageArray().get(i).getPackageApproved().equals("Y"))
{
arrayListCopy.getPackageArray().remove(i); //this is ArrayList<ClassType>. Nested class objects.
arrayListCopy.setTxtCustOk("OKOK"); //within the class
i--;
}
}
return arrayListCopy;
}
public class CustVoiceListObject implements Cloneable {
// ints, Doubles, Strings
private ArrayList<CustomerVoicePackageListObject> packageArray;
//getters & setters & constructors
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
@Override
protected Object clone() throws CloneNotSupportedException {
// First copy the easy stuff
CustomerVoiceListObject cvlo = (CustomerVoiceListObject)super.clone();
cvlo.packageArray = new ArrayList<CustomerVoicePackageListObject>(packageArray.size()); // Make sure it's the right size
for (CustomerVoicePackageListObject cvplo: packageArray) {
cvlo.packageArray.add(cvplo.clone());
} // for
return cvlo;
} // clone()