本帖最后由 陈嘉宾 于 2012-5-30 14:32 编辑
谁能帮我解释下这两段代码。。public boolean equals(Object obj)
{
if(!(obj instanceof person))
return false;
person p =(person)obj;
return this.name.equals(p.name) && this.age==p.age;
}
while(it.hasNext())
{
Object obj=it.next();
if(!newAl.contains(obj))
newAl.add(obj);
}
return newAl;
原代码
class person
{
private String name;
private int age
person(String name,int age)
{
this.name=name;
this.age=age;
}
public boolean equals(Object 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 al= new ArrayList();
al.add(new preson("lisi01",30));
al.add(new preson("lisi02",32));
al.add(new preson("lisi02",32));
al.add(new preson("lisi03",33));
al.add(new preson("lisi04",35));
al.add(new preson("lisi04",35));
Iterator it =al.iterator();
while(it.hasNext())
{
person p=(person)it.next();
sop(p.getName()+"::"+p.getAge());
}
}
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;
}
} |
|