I have an ArrayList that I want to clone and hence I made the following:
import java.util.ArrayList;
import java.util.Arrays;
public class Test {
public static ArrayList<ArrayList<Integer>> answer = new ArrayList<ArrayList<Integer>>();
public static ArrayList<ArrayList<Integer>> copans = new ArrayList<ArrayList<Integer>>();
public static void main(String[] args) {
ArrayList<Integer> yolo = new ArrayList<Integer>();
yolo.add(9);
yolo.add(0);
yolo.add(1);
answer.add(yolo);
appendRow();
}
static void appendRow() {
copans.addAll(answer);
copans.get(0).remove(0);
copans.get(0).remove(0);
System.out.println("ans "+answer);
}
}
appendRow() would result in copans becoming [1] from previously [9, 0, 1]. However, I did not expect that answer would become [1] as well instead of [9, 0, 1], which does not make sense at all.
I was wondering if I did not copy the values in the correct way? Thanks for your help!