泛型集合

泛型集合

集合也是泛型类

泛型集合的使用
优点:
获取类型时不用强转

实践:

package com.qianfeng.collection;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 功能描述
 *
 * @since 2022-05-12
 */
public class ArrayListDemo2 {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("hahaha");
        arrayList.add("heiheihei");
        for (String a : arrayList) {
            System.out.println(a);
        }

        ArrayList<Student> arrayList1 = new ArrayList<>();
        Student s1 = new Student("ZhangSan", 1);
        Student s2 = new Student("LiSi", 2);
        arrayList1.add(s1);
        arrayList1.add(s2);

        Iterator<Student> iterator = arrayList1.iterator();
        while (iterator.hasNext()) {
            Student next = iterator.next();
            System.out.println(next);
        }
    }
}
posted @ 2022-05-12 21:04  Oh,mydream!  阅读(23)  评论(0编辑  收藏  举报