- package com.wxy;
- import java.util.*;
- import javax.swing.text.html.HTMLDocument.Iterator;
- public class ColletionTest8
- {
- public static void main(String[] args)
- {
- ArrayList<Object> al = new ArrayList<Object>();
- al.add(new Person3("Steve", 23));
- al.add(new Person3("Steve", 23));
- al.add(new Person3("Larry", 21));
- al.add(new Person3("Gates", 26));
- al = singleElement(al);
- for(Object obj : al)
- {
- Person3 p =(Person3)obj;
- System.out.println("name:"+"="+p.getName() +"----"+"age:"+"="+p.getAge());
- }
-
- }
- private static ArrayList<Object> singleElement(ArrayList<Object> al)
- {
- ArrayList<Object> list = new ArrayList<Object>();
- java.util.Iterator<Object> it = al.iterator();
- while(it.hasNext())
- {
- Object obj = it.next();
- if(!(list.contains(obj)))
- {
- list.add(obj);
- }
- }
-
-
- return list;
- }
- }
- class Person3
- {
- private String name;
- private int age;
- public Person3(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
- @Override
- public boolean equals(Object obj)
- {
- if(!(obj instanceof Person3))
- return false;
- Person3 p = (Person3)obj;
- return (this.name.equals(p.name)) &&( this.age == p.age);
- }
- }
复制代码 |
|