- class ArrayListDemo{
- public static void main(String[] args){
- ArrayList al=new ArrayList();
- al.add(new Person("李四01",20));
- al.add(new Person("李四03",23));
- al.add(new Person("李四04",24));
- al.add(new Person("李四02",22));
- al.add(new Person("李四01",20));
- al.add(new Person("李四03",23));
- al.add(new Person("李四02",22));
- al=singleElement(al);
- Iterator it=al.iterator();
- while(it.hasNext()){
- Person p=(Person)it.next();
- System.out.println(p.getName()+p.getAge());
- }
- }
- public static ArrayList singleElement(ArrayList al){
- ArrayList la=new ArrayList();
- Iterator it=al.iterator();
- while(it.hasNext()){
- Object obj=it.next();
- if(!la.contains(obj))
- la.add(obj);
- }
- return la;
- }
- }
复制代码
用contains |