泛型集合的使用

package com.sean.base.generic;

import com.sean.base.collectionStudy.Student;

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

/**
 * 
 * @create 2021-02-25 14:11
 */
public class Demo02 {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("xxx");
        arrayList.add("xxx");
/*
        arrayList.add(10);
        arrayList.add(20);
*/
        for (String string:arrayList
             ) {
            System.out.println(string);

        }

        ArrayList<Student> arrayList2 = new ArrayList<Student>();
        Student s1 = new Student("sean",22);
        Student s2 = new Student("lee",42);
        Student s3 = new Student("beck",23);
        arrayList2.add(s1);
        arrayList2.add(s2);
        arrayList2.add(s3);

        Iterator<Student> it= arrayList2.iterator();
        while (it.hasNext()){
            Student s=it.next();
            System.out.println(s);
        }


    }
}

 

posted @ 2021-02-25 14:19  之樾  阅读(31)  评论(0)    收藏  举报