Removing an object from the duplicate ArrayList only

时光毁灭记忆、已成空白 提交于 2019-12-11 18:16:58

问题


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.


回答1:


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);

or

MyList2 = new ArrayList(MyList1);


来源:https://stackoverflow.com/questions/7543131/removing-an-object-from-the-duplicate-arraylist-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!