- import java.util.*;
- public class ArrayListTest2 {
- public static void main(String[] args) {
-
- ArrayList<Person> al = new ArrayList<Person>();
- al.add(new Person("lisi01",20));
- al.add(new Person("lisi02",21));
- al.add(new Person("lisi03",22));
- al.add(new Person("lisi04",23));
- al.add(new Person("lisi04",23));
-
- singleElements(al);
- Iterator<Person> it = al.iterator();
- while(it.hasNext())
- {
- Person p = it.next();
- System.out.println(p.getName()+"---"+p.getAge());
- }
- }
-
- public static ArrayList<Person> singleElements(ArrayList<Person> al)
- {
- ArrayList<Person> newAl = new ArrayList<Person>();
-
- Iterator<Person> it = newAl.iterator();
- while(it.hasNext())
- {
- Person p =it.next();
-
- if(!(newAl.contains(p)))
- newAl.add(p);
- }
- return newAl;
- }
- }
- class Person
- {
- private int age;
- private String name;
-
- public Person(String name,int age) {
- super();
- this.name = name;
- this.age = age;
-
- }
- @Override
- public boolean equals(Object obj)
- {
- if(!(obj instanceof Person))
- throw new RuntimeException("对象错误");
- Person p = (Person)obj;
- return this.name.equals(p.name) && this.age == p.age;
- }
-
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
-
-
- }
复制代码
为什么运行以后两个lisi都还在?求解? |
|