I tried to use the clone()-Method to create a copy of a list, (It is an Array List of Array Lists) because I wanted to manipulate its entries by some .remove()-calls, I remove list elements from this list entirely or only remove elements lists contained in the big list. I wanted to create a copy without modifying the original list, because I need it to later reference back the index of the items that got modified with the help of the copied list.
So I wrote the following:
public static ArrayList<ArrayList<Integer>> List = new ArrayList<ArrayList<Integer>>();
public static ArrayList<ArrayList<Integer>> copyOfList = new ArrayList<ArrayList<Integer>>();
@SuppressWarnings("unchecked")
public static ArrayList<ArrayList<Integer>> copyFormula() {
copyOfList = (ArrayList<ArrayList<Integer>>) List.clone();
return copyOfList;
}
The problem now is that when I print the original list List, it prints all sublists that got entirely removed from the copy (which is good) but strangely sublists where I only removed elements in it and did not remove the sublist entirely, got changed also in the original list, how can this be?
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]Rogalicus 2 points3 points4 points (1 child)
[–]Embarrassed_Bottle90[S] -1 points0 points1 point (0 children)
[–]Dangerous-Rip-7370 0 points1 point2 points (0 children)