package com.itheima;
import java.util.*;
class Person
{
private int age;
private String name;
Person(String name,int age)
{
this.age=age;
this.name=name;
}
public boolean equals(Object obj)怎么实现equalss调用的????????
{
if(!(obj instanceof Person ))
return false;
Person p=(Person)obj;
return this.name.equals(p.name)&&this.age==p.age;
}
public int GetAge()
{
return age;
}
public String name()
{
return name;
}
}
public class ArrayListTest2 {
public static void main(String[] args)
{
ArrayList al=new ArrayList();
al.add(new Person("lisi",22));
al.add(new Person("lisi",34));
al.add(new Person("lisi",23));
al.add(new Person("lisi",22));
al=singleElement(al);
Iterator it=al.iterator();
while(it.hasNext())
{
Person p=(Person)it.next();
System.out.println(p.GetAge()+p.name());
}
}
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;
}
} |