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

心有猛虎_細(xì)嗅薔薇
TA貢獻(xiàn)119條經(jīng)驗(yàn) 獲得超250個(gè)贊
當(dāng)初我也遇到過(guò)這個(gè)問(wèn)題。辦法是用Eclipse重新建個(gè)入口函數(shù)。將該代碼中的
剪切過(guò)去,就可以調(diào)試輸出了~