class Demo_ArrayList{ public static void main(Strintg[] args){
ArrayList<Person> list = new ArrayList<Person>();
list .add(new Person("张根硕";32));
list .add(new Person("张根硕";32));
list .add(new Person("古巨基";42));
list .add(new Person("古巨基";42));
list .add(new Person("阳顶天";45));
list .add(new Person("赵日天;36));
ArrayList<String> newlist =getSingle(list);
System.out.println(newlist);
}
public static ArrayList getSingle(ArrayList list){
ArrayList<Person> newlist = new ArrayList<Person>();
Iterator<String> it = list.iterator();
while(it.hasNext()){
Person p = it.next();
if(!newlist.contain(p)){
newlist.add(p);
}
}
return newlist;
}
}
public class Person {
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
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 String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
public boolean equals(Object obj) {
Person p =(Person)obj;
return this.name.equals(p.name) && this.age ==p.age;
}
}
|
|