按照视频操作步骤,eclipse上没有编写错误。run后提示找不到源代码或者下界指标越界,请问该如何修改?
package com.imooc_colleciton;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class SetTest {
public List<Course> coursesToSelect;
public void SetTest() {
coursesToSelect = new ArrayList<Course>();
}
/**
* 用于往coursesTOSelect中添加备选课程
*/
public void testAdd(){
//创建一个课程对象,并且通过调用add方法,添加到备选课程list中
Course Cr1=new Course("1","数据结构");
coursesToSelect.add(Cr1);
Course temp=(Course)coursesToSelect.get(0);
// System.out.println("添加了课程:"+temp.id+":"+temp.name);
Course Cr2=new Course("2","C语言");
coursesToSelect.add(0,Cr2);
Course temp2=(Course)coursesToSelect.get(0);
// System.out.println("添加了课程:"+temp2.id+":"+temp2.name);
// coursesToSelect.add(Cr1);
// Course temp0=(Course)coursesToSelect.get(2);
// System.out.println("添加了课程:"+temp.id+":"+temp.name);
//以下方法会抛出数组下标越界异常
// Course Cr3=new Course("3","test");
// coursesToSelect.add(3,Cr3);
Course[] course = {new Course("3","离散数学"),new Course("4","汇编语言")};
coursesToSelect.addAll(Arrays.asList(course));
Course temp3=(Course)coursesToSelect.get(3);
Course temp4=(Course)coursesToSelect.get(4);
// System.out.println("添加了两门课程:"+temp3.id+":"+
// temp3.name+";"+temp4.id+":"+temp4.name);
Course[] course2= {new Course("5","高等数学"),new Course("6","大学英语")};
coursesToSelect.addAll(2,Arrays.asList(course2));
Course temp5=(Course)coursesToSelect.get(2);
Course temp6=(Course)coursesToSelect.get(3);
// System.out.println("添加了两门课程:"+temp5.id+":"+
// temp5.name+";"+temp6.id+":"+temp6.name);
}
/**
* 通过for each方法访问集合元素
* @param args
*/
public void testForEach(){
System.out.println("有如下课程待选(通过for each访问):");
for(Object obj:coursesToSelect) {
Course Cr =(Course) obj;
System.out.println("课程:"+Cr.id+":"+Cr.name);
}
}
public static void main(String[] args) {
SetTest st = new SetTest();
st.testAdd();
st.testForEach();
//创建一个学生对象
Student student = new Student("1","小明");
System.out.println("欢迎学生:"+student.name+"选课!");
//创建一个scanner对象,用来接收从键盘输入的课程ID
Scanner console = new Scanner(System.in);
for(int i = 0 ; i<3 ; i++) {
System.out.println("请输入ID");
String courseId = console.next();
for(Course Cr : st.coursesToSelect) {
if(Cr.id.equals(courseId)) {
student.courses.add(Cr);
}
}
}
st.testForeachForSet(student);
}
public void testForeachForSet(Student student) {
//打印输出,学生所选的课程!
for(Course Cr :student.courses) {
System.out.println("选择了课程"+Cr.id+":"+Cr.name);
}
}
}