I have some data stored as ArrayList
. And when I want to backup this data,java bounds two objects forever. Which means when I change values in data ArrayL
I think you need to .clone()
the individual objects. Cloning the ArrayList
is not "deep"; it will only clone the references to the object.
Here is a fully functioning ArrayList backup class that checks if the ArrayList already exists. Basically it's just a for-loop cycling through the list and manually adding them to the new list.
import java.util.ArrayList;
public class Snapshot {
private ArrayList<Integer> dataBackup;
public Snapshot(ArrayList<Integer> data)
{
dataBackup = new ArrayList<Integer>();
for(int i = 0; i < data.size(); i++)
{
dataBackup.add(data.get(i));
}
}
public ArrayList<Integer> restore()
{
return dataBackup;
}
public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
Snapshot snap = new Snapshot(list);
list.set(0, 3);
list = snap.restore();
System.out.println(list); // Should output [1, 2]
list.add(4);
list = snap.restore();
System.out.println(list); // Should output [1, 2]
}
}
I'm assuming data
is the name of the ArrayList
you wanted to backup. If so, you should know that clone
is not deep - it only creates a copy of the object on which it is invoked, which in this case is the list. If it were a deep clone, it would fill the new list with clones of the objects in it.
Since it's not deep, if you change the objects the list contains, then the backup list will show those changes as well, since it's containing the same objects. The only time you'll not see changes in the backup after changing the "current" list is when you add or remove objects from the current list.
Some classes may override clone
to be deep, but not all. In general, it's not something you can rely on. When creating a backup copy of Java collections, remember to either clone the contained objects as well, or deal only with collections of immutable objects.
Regarding the cloning problem, once I solved this by serializing the entire collection into a string then serializing it back out into a new object. This forces you to make all your objects serializable and take when two objects really want to reference a single third object, but it can be a pretty good balance of simplicity and usefulness.
Actually, I haven't tried this but you could probably use a pipe to serialize in and out at the same exact time so that you aren't storing 3 copies in memory (if it's a huge collection)
You could write an object that wraps two ArrayLists. Anything write it so that it adds, removes, and modifies data in both at the same time.
It sounds (if I interpret your question properly; it's a little tough) like you're not copying the data that you're referring to in your ArrayList to your backup; you're copying the reference.
It's hard to say exactly how to solve your problem without knowing what your data type is that you're storing / backing up, but just be sure that you're copying the elements of the data contained within the ArrayList. That would mean, among other things, to do things like perform a clone() on the elements of the list, but not on the ArrayList (because that'll create a new cloned list with copies of the references to the same objects).