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) // 不理解这一部分……重写equals?
{
if (!(obj instanceof Person))
return false;
Person p = (Person)obj;
System.out.println(this.name+"===="+p.name);
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 o)
{
System.out.println(o);
}
public static void main(String[] args)
{
ArrayList al = new ArrayList();
al.add(new Person("weiwei001",20));//al.add(Object obj); Object obj = new Person("weiwei001",24);
al.add(new Person("weiwei002",22));
al.add(new Person("weiwei002",22));
al.add(new Person("weiwei003",23));
al.add(new Person("weiwei001",20));
al.add(new Person("weiwei004",20));
al = singleElement(al);
//sop("remove"+al.remove(new Person("weiwei003",23)));//都是使用equals方法进行比较的。
Iterator it = al.iterator();
while(it.hasNext())
{
Person p = (Person)it.next();
sop(p.getName()+":"+p.getAge());
}
}
public static ArrayList singleElement(ArrayList al)
{
ArrayList Al = new ArrayList();
Iterator it = al.iterator();