视频链接:13.22 泛型集合_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1zD4y1Q7Fw?spm_id_from=333.788.videopod.episodes&vd_source=b5775c3a4ea16a5306db9c7c1c1486b5&p=22
1.不使用泛型集合可能导致类型转换异常
类型转换异常举例:
2.泛型集合使用
public class GenericDemo01 {public static void main(String[] args) {ArrayList<String> arrayList = new ArrayList<>();arrayList.add("李四");arrayList.add("张三");
// arrayList.add(300);
// arrayList.add(100);for (String s : arrayList) {System.out.println(s);}ArrayList<Student> students = new ArrayList<>();Student s1 = new Student("张三",40);Student s2 = new Student("李四",41);Student s3 = new Student("王五",42);students.add(s1);students.add(s2);students.add(s3);Iterator<Student> iterator = students.iterator();while (iterator.hasNext()){Student s = iterator.next();System.out.println(s);}}
}