为什么注释处会报错?
package Collection;
import java.util.ArrayList;
import java.util.Iterator;
class Person
{
private String name;
private int age;
Person(String name , int age)
{
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
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 class SingleElementArrayList02
{
public static void main(String[] args)
{
ArrayList al = new ArrayList();
al.add( new Person("zhangsan" , 23) );
al.add( new Person("lisi" , 25) );
al.add( new Person("wangwu" , 27) );
al.add( new Person("zhangsan" , 23) );
al.add( new Person("wangwu" , 27) );
al.add( new Person("zhangsan" , 23) );
al = SingleElement(al);
Iterator it = al.iterator();
while( it.hasNext())
{
Person p = (Person)it.next();
sop("name:" + p.getName()+" "+"age:"+ p.getAge());
}
}
public static ArrayList SingleElement(ArrayList al)
{
ArrayList al1 = new ArrayList();
for(Iterator it = al.iterator(); it.hasNext();)
{
if( !al1.contains(it.next()))
{
al1.add(it.next());
}
}
return al;
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
|
|