ArrayList<String> values=new ArrayList<String>();
values.add("s");
values.add("n");
values.add("a");
values.add("s");
在此阵,我想删除重复的值。
In this Array, I want to remove repeated values.
如果你不想在集合重复,你应该考虑为什么你使用的集合,允许重复。删除重复的元素的最简单方法是将内容添加到一套(这将不允许重复),然后添加设回ArrayList的:
If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:
ArrayList al = new ArrayList();
// add elements to al, including duplicates
HashSet hs = new HashSet();
hs.addAll(al);
al.clear();
al.addAll(hs);
最重要的是,这会破坏ArrayList中元素的顺序。
most importantly, this destroys the ordering of the elements in the ArrayList.