public static void main(String[] args) {
// 创建集合对象
ArrayList al = new ArrayList();
al.add(new Student("小白", 18));
al.add(new Student("小黑", 18));
al.add(new Student("小白", 18));
al.add(new Student("小明", 18));
al.add(new Student("小白", 19));
// 创建新集合
ArrayList al2 = new ArrayList();
// 遍历集合1
Iterator it = al.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "\t" + s.getAge());
if (!al2.contains(s)) {
al2.add(s);
}
}
System.out.println("*******************");
// 遍历集合2
for (int i = 0; i < al2.size(); i++) {
Student s = (Student) al2.get(i);
System.out.println(s.getName() + "\t" + s.getAge());
}
} |