仅从重复的 ArrayList 中删除对象
我已经复制了一个 ArrayList,如下所示:
MyList2 = MyList1;
尝试将 MyList2 的对象与 MyList1 拥有的对象一起加载。
现在,当我迭代 MyList2 时,我 it.remove() 一些对象,但这会导致通过 MyList1 的父迭代上的其他地方出现并发修改异常。我认为当我 it.remove() 时,它实际上也将其从原始 ArrayList 中删除,如何仅从 MyList2 中删除它?谢谢。
I've copied an ArrayList over as so:
MyList2 = MyList1;
In an attempt to load MyList2's objects with the ones which MyList1 has.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是您尚未创建 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 door