我有一个需要删除重复项然后将列表复制到另一个列表中的项目列表。问题是我无法将列表复制到另一个列表中。
代码
.....
private List mylist = new ArrayList();
.....
LinkedHashSet hs = new LinkedHashSet();
hs.addAll(mylist);
mylist.clear();
mylist.addAll(hs);
MyClass.getItems().clear();
MyClass.setItems(mylist);
MyClass.java
.....
private List Items = new ArrayList();
public void setItems(List myItems) {
for (int i = 0; i < myItems.size(); i++) { <<This loop shows the items
System.out.println(myItems.get(i));
}
this.Items.clear();
this.Items.addAll(myItems);
for (int i = 0; i < Items.size(); i++) { << this loop does not show anything
System.out.println(Items.get(i));
}
}
期望的结果
mylist >> a,b,c,a,d,c
change to a,b,c,d
then copy to items
items >> a,b,c,d