import java.util.*;
public class acc
{
public static void main(String argv[])
{
ArrayList al=new ArrayList();
al.add("java1");
al.add("java2");
al.add("java2");
al.add("java3");
al.add("java4");
al.add("java4");
sop(al);
singleElements(al);
sop(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;
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
结果:[java1, java2, java2, java3, java4, java4]
[java1, java2, java2, java3, java4, java4]
目的是去除重复的元素,但是怎么跟原来一样呢? |
|