class ArrayListTest
{
public static void sop(Object obj){
System.out.println(obj);
}
public static void main(String[] args)
{
ArrayList al= new arrayList();
al.add("book01");
al.add("book02");
al.add("book01");
al.add ("book04");
al=singleelement(al);
sop(al);
}
public static ArrayList singleelement(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;
}
}
------解决思路----------------------
ArrayList al= new arrayList();-->ArrayList al= new ArrayList();
a大写
------解决思路----------------------
public class ArrayListTest {
public static void sop(Object obj) {
if (obj instanceof List) {
for (Iterator<Object> it = ((List) obj).iterator(); it.hasNext();)
sop(it.next());
} else if (obj.getClass().isArray()) {
for (int i = 0, len = Array.getLength(obj); i < len; i++)
sop(Array.get(obj, i));
} else {
System.out.println(obj);
}
}
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add("book01");
al.add("book02");
al.add("book01");
al.add("book04");
al = singleelement(al);
sop(al);
}
public static ArrayList singleelement(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;
}
}