In this case, you will need to make a deep copy of the list, i.e. a copy that does not copy the links, but actually copies the object that the links point to.
Collections.copy "copies all items from one list to another." As usual in Java, list items are not objects, but links.
This can be solved by implementing Cloneable (and using .clone() ) or by creating your own "copy constructor", which takes the object to be copied as an argument and creates a new object based on the data from this argument. No matter which option you choose, you will have to iterate over the list and make a copy for each object.
Here is an example that uses the copier constructor approach:
MyContacts.medicalContacts = new ArrayList(); for (Contact c: MyContacts.attackContacts) medicalContacts.add(new Contact(c));
Related Question:
source share