本帖最后由 王博 于 2012-12-11 15:24 编辑
import java.util.*; class Person { private String name; private int age; Person(String name,int age) { this.name=name; this.age=age; } public boolean equals(Object obj) { if (!(obj instanceof Person)) //这里是什么意思啊??毕老师讲的时候没有听懂啊?? { //能否详细讲解一下 return false; } Person p=(Person)obj; return this.name.equals(p.name)&&this.age==p.age; } public String getName() { return name; } public int getAge() { return age; } } class ArrayListTest2 { public static void main(String[] args) { ArrayList al = new ArrayList(); al.add(new Person("List01",30)); al.add(new Person("List02",32)); al.add(new Person("List03",33)); al.add(new Person("List02",32)); 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 newAl = new ArrayList(); Iterator it = al.iterator(); while (it.hasNext()) { Object obj=it.next(); if (!newAl.contains(obj)) { newAl.add(obj); } } return newAl; } } |