本帖最后由 李春生 于 2012-11-20 14:30 编辑
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)
{
System.out.println(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 sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
ArrayList a1 = new ArrayList();
a1.add(new Person("yuanfang01",25));
a1.add(new Person("yuanfang01",25));
a1.add(new Person("yuanfang02",24));
a1.add(new Person("yuanfang02",24));
a1.add(new Person("yuanfang03",23));
a1.add(new Person("yuanfang04",21));
a1 = singleElement(a1);
Iterator it = a1.iterator();
while(it.hasNext())
{
Person p = (Person)it.next();
sop(p.getName()+"::"+p.getAge());
}
}
}
红色部分为什么不通过
|
|