import java.util.ArrayList;标签:iterator,ArrayList,add,集合,arrayList1,Student,泛型,new From: https://www.cnblogs.com/123456dh/p/17136604.html
import java.util.Iterator;
public class Demo01 {
public static void main(String[] args) {
//泛型的好处:1.提高代码的重用性 2.防止类型转换异常,提高代码的安全性
//泛型集合
ArrayList<String> arrayList = new ArrayList<>();//不能再添加String类型之外的其他引用类型
arrayList.add("123");
arrayList.add("456");
for (String s:
arrayList) {
System.out.println(s);
}
ArrayList<Student> arrayList1 = new ArrayList<>();
Student s1 = new Student("张三", 16);
Student s2 = new Student("李四", 16);
Student s3 = new Student("王五", 16);
arrayList1.add(s1);
arrayList1.add(s2);
arrayList1.add(s3);
Iterator<Student> iterator = arrayList1.iterator();
while (iterator.hasNext()){
Student next = iterator.next();
System.out.println(next);
}
}
}