//返回List合適些
public static ArrayList singleElement(ArrayList al){
//定義一個臨時容器
ArrayList newAl = new ArrayList();
//在迭代是循環中next調用一次,就要hasNext判斷一次
Iterator it = al.iterator();
while (it.hasNext()){
Object obj = it.next();//next()最好調用一次就hasNext()判斷一次否則容易發生異常
if (!newAl.contains(obj))
newAl.add(obj);
}
return newAl;
}
}
新聞熱點
疑難解答