package com.imooc.collection;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Scanner;public class Settest { public List<Course>coursesToselect; public Settest(){ coursesToselect=new ArrayList<Course>(); } 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); //以下方法会抛出数组越界异常 //Course cr3=new Course("3","java"); // coursesToSelect.add(4,cr3); Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")}; coursesToselect.addAll(Arrays.asList(course)); Course temp3=(Course)coursesToselect.get(2); Course temp4=(Course)coursesToselect.get(3); //System.out.println("添加了两门课程:"+temp3.id+":"+temp3.name // +temp4.id+":"+temp4.name); Course[] course2={new Course("5","大学英语"), new Course("6","高等数学")}; coursesToselect.addAll(Arrays.asList(course2)); Course temp5=(Course)coursesToselect.get(4); Course temp6=(Course)coursesToselect.get(5); //System.out.println("添加了两门课程:"+temp5.id+":"+temp5.name // +temp6.id+":"+temp6.name); } //System.out.println("添加了两门课程:"temp5.id+":"+temp5.name+temp6.id+":"+temp6.name); public void testGet(){ int size=coursesToselect.size(); System.out.println("有如下课程待选 :"); for(int i=0; i<size;i++){ Course cr=(Course) coursesToselect.get(i); //System.out.println("课程"+cr.id+":"+cr.name); } } public void testForEach(){ System.out.println("有如下课程待选 (通过ForEach访问)"); for(Object obj:coursesToselect){ Course cr=(Course) obj; System.out.println("课程"+cr.id+":"+cr.name); } } public static void main(String[] args) { // TODO 自动生成的方法存根 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.testForEachSet(student); } public void testForEachForSet(Student student2){ //打印输出学生所选的课程 for (Course cr:student2.courses){ System.out.println("选择了课程:"+cr.id+":"+cr.name); } } } private void testForEachSet(Student student) { // TODO 自动生成的方法存根 } private void testForEachSet() { // TODO 自动生成的方法存根 }}
添加回答
举报
0/150
提交
取消