package student_progress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class SetTest {
public List<Course>courseToSelect;
private Scanner scanner;
public Student student;
public SetTest() {
courseToSelect=new ArrayList<Course>();
this.scanner=new Scanner(System.in);
}
public void Add() {
//创建一个课程对象,并通过调用add方法添加到备选课程List中
Course[] courses= {new Course(1, "数据结构"),new Course(2, "c语言"),new Course(3, "离散数学"),new Course(4, "汇编语言"),
new Course(5, "高等数学"),new Course(6, "大学英语")};
courseToSelect.addAll(Arrays.asList(courses));
}
public void testIterator() {
Iterator<Course> it=courseToSelect.iterator();
System.out.println("有如下课程待选(通过迭代器访问):");
while(it.hasNext()) {
Course cr=(Course) it.next();
System.out.println("课程"+cr.id+":"+cr.name);
}
}
//创建学生对象并选课
public void createStudentAndSelectCourse() {
student=new Student(1, "小明");
System.out.println("欢迎学生"+student.name+"来选课");
for(int i=0;i<3;i++) {
System.out.println("请输入课程ID");
int courseId=scanner.nextInt();
for(Course cr:courseToSelect) {
if(cr.id==courseId) {
student.courses.add(cr);
}
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SetTest sTest=new SetTest();
sTest.Add();
sTest.testContains();
sTest.testIterator();
}
public void testForeachForSet(Student student) {
//打印输出,学生所选的课程
System.out.println("共选择了:"+student.courses.size()+"课程");
for(Course cr:student.courses) {
System.out.println("选择了课程:"+cr.id+":"+cr.name);
}
}
//测试List的contains方法
public void testContains() {
//请输入课程名称
System.out.println("请输入课程名称:");
int id1=scanner.nextInt();
Course course2=new Course();
//创建一个新的课程对象,ID和名称,与Course对象完全一样
course2.id=id1;
System.out.println("创建课程对象编号:"+course2.id);
System.out.println("备选课程中是否包含该课程:"+course2.id+","+courseToSelect.contains(course2));
if(courseToSelect.contains(course2)) {
System.out.println("课程:"+course2.id+"索引位置为:"+courseToSelect.indexOf(course2)+1);
}
}
}
明白了,我在equals那里没有改