I am using two java List objects that contain some data.
the first list contains all the data objects inside it and teh second one contains some of the data(not all) from the original list.
The original list itself is a static object that can be accessed .
Here is the code below that copies the whole contents of the original list into a new List and then ammends the copied list my removing certain elements.
The issue i am having is that it seems to effect and remove same elements from the Original list!
private List deviceList;
deviceList = App.devices;
//check which devices have not been added by checking the position data
for (Iterator iterator = deviceList.iterator(); iterator.hasNext(); ) {
Device device = iterator.next();
if (device.getPosition() != Device.NO_POSITION) {
iterator.remove();
}
}
解决方案
In this line of code:
deviceList = App.devices;
you are not copying the list, but creating another reference to it.
To make a shallow copy of the list you can use for example: ArrayList constructor which accepts Collection as a parameter and makes copy.
So it should be like that:
private List deviceList = new ArrayList(App.devices);