亲,给你一个例子哦:- import java.util.*;
- public class ListDemo {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ArrayList<Person> list = new ArrayList<Person>();
- list.add(new Person("01","张三",10,170));
- list.add(new Person("02","李四",20,175));
- list.add(new Person("03","马五",20,175));
- list.add(new Person("04","王六",20,175));
- list.add(new Person("05","孙七",23,180));
- list.add(new Person("06","陈八",23,180));
- list.add(new Person("07","赵九",30,160));
- list.add(new Person("08","吴十",30,160));
- list = singleElement(list);
- Iterator<Person> it= list.iterator();
- while(it.hasNext())
- {
- Person p = it.next();
- System.out.println(p.getid() + " " + p.getName() + " " + p.getAge() + " " + p.getHeight() );
-
- }
-
- }
- public static ArrayList singleElement(ArrayList<Person> al)
- {
- ArrayList<Person> newAl = new ArrayList<Person>();
- Iterator<Person> it = al.iterator();
- while(it.hasNext())
- {
- Person p = it.next();
- if(!newAl.contains(p))
- newAl.add(p);
- }
- return newAl;
- }
- }
- class Person
- {
- private String id;
- private String name;
- private int age;
- private int height;
- Person(String id,String name,int age,int height)
- {
- this.id = id;
- this.name = name;
- this.age = age;
- this.height = height;
- }
- public String getid()
- {
- return id;
- }
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- public int getHeight()
- {
- return height;
- }
- public boolean equals(Object obj)
- {
- if(!(obj instanceof Person))
- return false;
- Person p = (Person)obj;
- return this.age == p.age && this.height == p.height;
-
- }
-
- }
复制代码 |