为什么testForeach() 函数中 Course cr : student.courses 会报错?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | package com.imooc.stu; import java.util.ArrayList; import java.util.List; import java.util.*; public class setTest { public List<Course> coursetoSelect; public setTest() { this .coursetoSelect = new ArrayList<Course>(); } public void crAdd(){ Course[] cr = { new Course( "1" , "语文" ), new Course( "2" , "数学" ), new Course( "3" , "英语" ), new Course( "4" , "物理" ), new Course( "5" , "生物" )}; coursetoSelect.addAll(Arrays.asList(cr)); } public void crDisplay() { System.out.println( "有如下待选课程:" ); for (Course cr:coursetoSelect) { System.out.println(cr.id + " " + cr.name); } } public static void main(String[] args) { // TODO Auto-generated method stub setTest st = new setTest(); st.crAdd(); st.crDisplay(); Student student = new Student( "1" , "小明" ); System.out.println( "欢迎学生:" + student.name); Scanner sc = new Scanner(System.in); for ( int i = 0 ;i< 3 ;i++){ System.out.println( "请输入选择课程ID:" ); String scc = sc.next(); for (Course cr : st.coursetoSelect) { if (cr.id.equals(scc)) { student.courses.add(cr); } } } st.testForeach(student); } public void testForeach(Student student) { for (Course cr : student.courses){ System.out.println(cr.id + "" + cr.name); } } } |