ArrayList实现删除重复元素(元素不是对象类型的情况)
package 集合;
import java.util.ArrayList;
import java.util.Iterator;
/*
* 去除ArrayList里面的重复元素
*
* */
public class ArrayListTest1 {
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add("java1");
al.add("java2");
al.add("java1");
al.add("java2");
al.add("java1");
al.add("java3");
sop(al);
al = noReplaceElement(al);
sop(al);
}
public static void sop(Object obj){
System.out.print(obj);
}
public static ArrayList noReplaceElement(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;
}
}