I need to write out a method to clone a nested ArrayList
.
The nested ArrayList
looks like this:
ArrayList<ArrayList<Ship>> theSea = new ArrayList<ArrayList<Ship>>();
I want to copy it to a clone:
ArrayList<ArrayList<Ship>> seaClone = new ArrayList<ArrayList<Ship>>();
I've tried iterating it and copying over the lists:
for(int i = 0; i < theSea.size(); i++){
seaClone.add(theSea.get(i));
}
However, this doesn't clone the elements of the nested ArrayList<Ship>
and instead just copies over a reference to it.
How should I go about cloning the elements of the nested ArrayList<Ship>
?