為啥報錯呀
import java.util.ArrayList;
import java.util.List;
public class TestGeneric {
//帶有泛型---Course的List類屬性
public List<Course> course;
private ArrayList<Course> courses;
public TestGeneric(){
this.courses = new ArrayList<Course>();
}
public void testAdd() {
Course cr1 = new Course("1","大學語文");
courses.add(cr1);
//泛型集合中,不能添加泛型規定的類型以外的對象,否則會報錯
//courses.add("我是亂入的哈!");
Course cr2 = new Course("2","Java基礎");
courses.add(cr2);
}
//通過foreach方法訪問集合元素
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);
}
缺了一個大括號}