package com.imooc.collection;import java.util.ArrayList;import java.util.List;public class TestGeneric { public static void main(String[] args) { TestGeneric tg=new TestGeneric(); tg.testAdd(); tg.testForEach(); // TODO 自動(dòng)生成的方法存根 /* * 帶有泛型————Course,的List類型屬性; */ } private void testForEach() { // TODO 自動(dòng)生成的方法存根 } public ?List<Course> courses;? ? ? public TestGeneric(){? ? ?this.courses=new ArrayList<Course>(); ? ? ?? ? ?}; ? ? ?/*? ? ? * 測試添加? ? ? */? ? ?public void testAdd(){? ? Course cr1=new Course("1","大學(xué)英語");? ? courses.add(cr1);? ? //泛型集合中不能添加泛型規(guī)定類型以外的對象;否則會(huì)報(bào)錯(cuò);? ? //courses.addAll("能否添加一些奇怪的數(shù)字呢");? ? Course cr2=new Course("2","java基礎(chǔ)");? ? courses.add(cr2);? ? ?? ? //測試循環(huán)遍歷? ? public void testForEach(){? ? for (Course cr:courses){? ? System.out.println(cr.id+":"+cr.name);? ? }? ? }? ? ?}}
添加回答
舉報(bào)
0/150
提交
取消