仅从重复的ArrayList中删除对象 [英] Removing an object from the duplicate ArrayList only
问题描述
我已经复制了一个ArrayList:
I've copied an ArrayList over as so:
MyList2 = MyList1;
尝试使用MyList1具有的对象加载MyList2的对象。
In an attempt to load MyList2's objects with the ones which MyList1 has.
现在,当我遍历MyList2时,我it.remove()一些对象,但这会导致通过MyList1在父迭代的其他地方发生并发修改异常。我想当它it.remove()它实际上也从原始的ArrayList中删除它,如何从MyList2中删除它?谢谢。
Now as I iterate through MyList2, I it.remove() some objects, but this is causing a concurrent modification exception elsewhere on the parent iteration through MyList1. I think when i it.remove() it's actually removing it from the original ArrayList as well, how do remove it only from MyList2? Thanks.
推荐答案
你的问题是你还没有创建 ArrayList的副本
,有两个对同一对象的引用。如果你想复制列表,那么你可以做
Your problem there is that you haven´t created a copy of the ArrayList
, there are two references to the same object. If you want to copy the list, then you could do
Collections.copy(MyList2,MyList1);
或
MyList2 = new ArrayList(MyList1);
这篇关于仅从重复的ArrayList中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!