- class Person
- {
- private String name;
- private int age;
- Person(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- public boolean equals(Object obj) //为什么这里只要复写这一个方法而已啊 为什么hashCode 为什么不要
- {
- if(!(obj instanceof Person))
- return false;
- Person p = (Person)obj;
- return this.name.equals(p.name) && this.age== age;
-
- }
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- }
- class Demo
- {
- public static void main(String[] args)
- {
- ArrayList ar = new ArrayList();
- ar.add(new Person("zhangsan01",21));
- ar.add(new Person("zhangsan02",22));
- ar.add(new Person("zhangsan03",23));
- ar.add(new Person("zhangsan03",23));
- ar.add(new Person("zhangsan04",24));
- ar.add(new Person("zhangsan04",24));
- ar = show(ar);
-
- Iterator it = ar.iterator();
- while(it.hasNext())
- {
- Object obj = it.next();
- Person p = (Person)obj;
- System.out.println(p.getName()+";;;;"+p.getAge());
- }
- }
- public static ArrayList show(ArrayList ar)
- {
- ArrayList lis = new ArrayList();
-
- Iterator it = ar.iterator();
- while(it.hasNext())
- {
- Object obj =it.next();
- if (!lis.contains(obj))
- lis.add(obj);
-
- }
- return lis;
- }
- }
复制代码 |