I have two identical ArrayLists, say l1 and l2.
Both are multi-dimensional, for instance ArrayList<ArrayList<Integer>>.
When I create and populate l1, I'm using new ArrayList<>(l1); to initialize l2. If these were ArrayLists of a single dimension, me editing l2 would leave l1 unchanged, but since they are not, adding a new Integer to l2 also changes l1. If I were to add a new ArrayList to it, though, l1 wouldn't do anything.
What can be done to make sure that changing an element in l2 doesn't tamper with l1?
Example:
ArrayList<ArrayList<Integer>> l1 = new ArrayList<>();
for (var x = 0; x < 10; x++) {
l1.add(new ArrayList<>());
for (var y = 0; y < 10; y++) {
l1.get(x).add(y);
}
}
ArrayList<ArrayList<Integer>> l2 = new ArrayList<ArrayList<Integer>>(l1);
//Should not change l1
l2.get(0).add(999);