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)//复写Object类中的equals方法 就这个方法搞不懂求解救
{
if(!(obj instanceof Person))
{
return false;
}
Person p=(Person)obj;
return this.name.equals(p.name)&&this.age==age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
class ArrayListTast
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
ArrayList al=new ArrayList();
al.add(new Person("lisi01",23));
al.add(new Person("lisi01",23));
al.add(new Person("wangwu",22));
al.add(new Person("lisi01",23));
al.add(new Person("lisi02",25));
al.add(new Person("lisi01",23));
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();
while(it.hasNext())
{
Object obj=it.next();
if(!Al.contains(obj))
Al.add(obj);
}
return Al;
}
}
|
|