我尽力而为,但总是引用前一个。
mListPreviousData = new ArrayList<>();
for (int i = 0; i < mAddedList.size(); i++) {
mListPreviousData.add(mAddedList.get(i));
}
答案 0 :(得分:1)
您需要制作基础对象的深层副本:
mListPreviousData = new ArrayList<>();
for (int i = 0; i < mAddedList.size(); i++) {
mListPreviousData.add(mAddedList.get(i).clone());
}
最好是实施自己的clone()
方法。
编辑:
或者对整个arraylist进行深度复制:
mListPreviousData = (..cast to your type..) mAddedList.clone();
答案 1 :(得分:1)
mListPreviousData = new ArrayList<>();
mListPreviousData.addAll(mAddedList);