package com.oop.collection;
import lombok.Data;
@Data
public class Student {
private String name;
private int age;
public Student(){}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
package com.oop.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo2 {
public static void main(String[] args) {
Collection collection = new ArrayList();
Student s1 = new Student("张三", 20);
Student s2 = new Student("李四", 18);
Student s3 = new Student("王五", 22);
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println("元素个数:" + collection.size());
System.out.println(collection.toString());
System.out.println("------3.1使用增强for-------");
for (Object object : collection) {
Student s = (Student)object;
System.out.println(s.toString());
}
System.out.println("------3.2使用迭代器-------");
Iterator it = collection.iterator();
while (it.hasNext()){
Student s = (Student) it.next();
System.out.println(s.toString());
}
System.out.println(collection.contains(s1));
System.out.println(collection.isEmpty());
}
}