import java.util.*;
ArrayListTest
{
public static void main(String[] args)
{
ArrayList al= new ArrayList();
al.add("java01");
al.add("java02");
al.add("java03");
al.add("java04");
al.add("java03");
al.add("java04");
sop(al);
al=singleElement(al);
sop(al);
}
public static ArrayList singleElement(ArrayList al)
{
ArrayList newAl=new ArrayList();
Iterator it=al.iterator();
while(it.hasNext())
{
//Object obj=(Object)it.next();
//if(!newAl.contains(obj)
//newAl.add(obj);
}
return newAl;
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
上边注释的代码可不可以写成这样
if(!newAl.contains(it.next());
newAl.add(it.next());
|
|