a1 = singleElement(a1);
Iterator it = a1.iterator();
while(it.hasNext())
{
Person p = (Person)it.next();
sop(p.getName()+"----"+p.getAge());
}
}
public static ArrayList singleElement(ArrayList a1)
{
ArrayList newA1 = new ArrayList();
Iterator it = a1.iterator();
while(it.hasNext())
{
Person p = (Person) it.next();
a1 = singleElement(a1);
Iterator it = a1.iterator();
while(it.hasNext())
{
Person p = (Person)it.next();
sop(p.getName()+"----"+p.getAge());
}
}
public static ArrayList singleElement(ArrayList a1)
{
ArrayList newA1 = new ArrayList();
Iterator it = a1.iterator();
while(it.hasNext())
{
Person p = (Person) it.next();
//contains()这个方法内部调用的就是equals方法,所以Person类中要重写equals方法(比较姓名和年龄)
//equals方法如果不重写的话,比较的是对象的hashCode
if(!newA1.contains(p))
{
newA1.add(p);
sop("add :"+p.getName());
}
}
return newA1;
}
}
class Person
{
private String name;
private int age;
public boolean equals(Object obj)
{
if(!(obj instanceof Person))
{
return false;
}
Person p = (Person)obj;