public class ArrayListDemo3 {
public static void main(String[] args) {
//创建对象集合
ArrayList<Student> al = new ArrayList<Student>();
ArrayList<Student> newAl = new ArrayList<Student>();
//给集合中的对象赋值
al.add(new Student("小华",28));
al.add(new Student("小花",18));
al.add(new Student("小李",19));
al.add(new Student("小张",38));
al.add(new Student("小华",28));
//遍历元素
//普通for循环遍历
for(int i = 0; i < al.size(); i++){
Student s = al.get(i);
if(!newAl.contains(s)){
newAl.add(s);
}
}
//遍历新集合
for (int x = 0; x < newAl.size(); x++) {
Student s2 = newAl.get(x);
System.out.println(s2);
}
}
}
|
|