public class 第二题 {
public static void main(String[] args) {
List <Person>list1 = new ArrayList<Person>();
list1.add(new Person("漩涡鸣人",18));
list1.add(new Person("漩涡鸣人",18));
list1.add(new Person("日向雏田",16));
list1.add(new Person("宇智波佐助",19));
list1.add(new Person("宇智波佐助",19));
list1.add(new Person("宇智波佐助",19));
list1.add(new Person("长门",50));
list1.add(new Person("长门",50));
list1.add(new Person("长门",50));
List <Person>list2 = new ArrayList<Person>();
for (int i = 0; i < list1.size(); i++) {
if (!list2.contains(list1.get(i))) {
list2.add(list1.get(i));
}
}
System.out.println(list2);
}
}
------------------------------------------------------------------------------------------------------------手动分割线
Person类 代码区
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;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
@Override
public boolean equals(Object obj) {
Person p = (Person)obj;
return this.name.equals(p.name) && this.age == p.age;
}
}