為啥報(bào)錯(cuò)呀
import java.util.ArrayList;
import java.util.List;
public class TestGeneric {
//帶有泛型---Course的List類(lèi)屬性
public List<Course> course;
private ArrayList<Course> courses;
public TestGeneric(){
this.courses = new ArrayList<Course>();
}
public void testAdd() {
Course cr1 = new Course("1","大學(xué)語(yǔ)文");
courses.add(cr1);
//泛型集合中,不能添加泛型規(guī)定的類(lèi)型以外的對(duì)象,否則會(huì)報(bào)錯(cuò)
//courses.add("我是亂入的哈!");
Course cr2 = new Course("2","Java基礎(chǔ)");
courses.add(cr2);
}
//通過(guò)foreach方法訪問(wèn)集合元素
public void testForEach() {
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();
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:?
Syntax error, insert "}" to complete Block
at com.collection.TestGeneric.testForEach(TestGeneric.java:26)
at com.collection.TestGeneric.main(TestGeneric.java:31)
2018-10-09
public void testForEach() {
for(Course cr:courses) {
System.out.println("課程-->" + cr.id + ":" + cr.name);
}
缺了一個(gè)大括號(hào)}