The code copy is as follows:
class ArrayListTest1 {
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add("java03");
al.add("java03");
al.add("java01");
al.add("java02");
al.add("java01");
al.add("java02");
al.add("java01");
System.out.println(al);
al = singleElement(al);
System.out.println(al);
}
//Return to List is appropriate
public static ArrayList singleElement(ArrayList al){
//Define a temporary container
ArrayList newAl = new ArrayList();
//In the iteration, next is called once in the loop, and hasNext must be judged once.
Iterator it = al.iterator();
while (it.hasNext()){
Object obj = it.next();//Next() is best to call it once and judge hasNext() once, otherwise an exception is prone to occur.
if (!newAl.contains(obj))
newAl.add(obj);
}
return newAl;
}
}