0
07
15
14
为了账号安全,请及时绑定邮箱和手机立即绑定

为什么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);
        }
         
    }
     
 
}


正在回答

代码语言

1 回答

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.imooc.stu;
import java.util.*;
 
public class Student {
    
    public String id;
    public String name;
     
    public Set<Course> courses;
     
    public Student(String id, String name) {
         
        this.id = id;
        this.name = name;
         
        this.courses = new HashSet<Course>();
         
    }
     
}

原来是Student类没有定义泛型  public Set<Course> courses;

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

为什么testForeach() 函数中 Course cr : student.courses 会报错?

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号