大家帮我看一下,我为什么运行的时候什么也没有呢。
package day14;
/*
* 描述人对象,将人的数据封装对象
* 定义一个容器,将对象存入
* 取出
* */
import java.util.*;
public class ArrayListTest2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList al = new ArrayList();
al.add(new Person("lisi01",21));
al.add(new Person("lisi02",22));
al.add(new Person("lisi03",23));
al.add(new Person("lisi04",24));
al.add(new Person("lisi01",21));
al.add(new Person("lisi02",22));
al = singleElement(al);
Iterator it = al.iterator();
while(it.hasNext()){
Person p = (Person)it.next();
System.out.println(p.getAge()+"::"+p.getName());
}
}
public static ArrayList singleElement(ArrayList al){
ArrayList newAl = new ArrayList();
Iterator it = al.iterator();
while(it.hasNext()){
Object obj = it.next();
if(newAl.contains(obj)){
newAl.add(obj);
}
}
return newAl;
}
}
class Person{
private String name;
private int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
public boolean equals(Object obj){
if(obj instanceof Person)
return false;
Person p = (Person)obj;
return this.name.equals(p.name) && this.age==p.age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
} |
|