欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > 集合框架11:泛型集合

集合框架11:泛型集合

2025/5/24 18:48:02 来源:https://blog.csdn.net/zhangjinajian759/article/details/143029573  浏览:    关键词:集合框架11:泛型集合

视频链接:13.22 泛型集合_哔哩哔哩_bilibiliicon-default.png?t=O83Ahttps://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);}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词