class Demo_ArrayList{ public static void main(String[] args){
ArrayList<String> list = new ArrayList<String>(); //创建一个泛型为String的ArrayList集合
list.add("a"); //向集合中添加元素
list.add("b");
list.add("a");
list.add("c");
ArryList<String>newlist = getSingle(liist); //创建一个新集合
System.out.println(newlist);
}
public static ArrayList getSingle(ArrayList list){
Iterator<String> it = list.iterator(); //获取迭代器
ArrayList<String> newlist = new ArrayList<String>();
while(it.hasNext()){
Object obj = it.next();
if(!newlist.contains(obj)){
newlist.add(obj)
}
}
return newList;
}
}
|
|