import java.util.*;
class Preson2
{
private String name ;
private int age;
Preson2(String name ,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public boolean equlas(Object obj)
{
if(!(obj instanceof Preson2))
return false;
Preson2 p =(Preson2)obj;
return this.name.equals(p.name)&&this.age==p.age;
}
}
public class Demo1
{
public static void main(String[]args)
{
ArrayList al=new ArrayList();
al.add(new Preson2("java01",20));
al.add(new Preson2("java02",30));
al.add(new Preson2("java03",10));
al.add(new Preson2("java03",10));
al.add(new Preson2("java03",10));
al=xinal2(al);
Iterator it =al.iterator();
while(it.hasNext())
{
Preson2 p= (Preson2)it.next();
sop(p.getName()+"..."+p.getAge());
}
}
public static ArrayList xinal2(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;
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
去除相同元素 哪里错了? 找了半天没找着哪有问题 。 |
|