/*
去除ArrayList中的重复元素
*/
import java.util.*;
class ArrayListTest
{
public static void main(String[] args)
{
ArrayList al = new ArrayList();
al.add("java01");
al.add("java01");
al.add("java02");
al.add("java03");
al.add("java03");
al.add("java04");
al.add("java06");
al.add("java06");
System.out.println(al);
al= singleElements(al);
System.out.println(al);
}
public static ArrayList singleElements(ArrayList al)
{
ArrayList newAl = new ArrayList();
Iterator it = al.iterator();
while(it.hasNext())
{
Object obj = it.next();
if(!newAl.contains(obj))
newAl.add(obj);
}
return newAl;
}
}
|
|