java test报错_SetTest.java文件报错

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 coursesToSelect;

public SetTest() {

coursesToSelect = new ArrayList();

}

public void testAdd() {

//创建一个课程对象,并通过调用add方法,添加到备选课程list中。

Course cr1 = new Course("1","数据结构");

coursesToSelect.add(cr1);

Course temp = (Course)coursesToSelect.get(0);//对象存入集合都变成Object类型,取出时需要类型转换

//        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[] 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);

}

public void testForEach() {

System.out.println("有如下课程待选(通过for each访问):");

for (Object obj:coursesToSelect) {

Course cr = (Course)obj;

System.out.println("课程:"+cr.id+":"+cr.name);

}

}

public void testForEachForSet(Student student) {

//打印输出学生所选的课程

for(Course cr:student.courses) {

System.out.println("选择了课程:"+cr.id+":"+cr.name);

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

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);

student.courses.add(cr);

}

}

}

st.testForEachForSet(student);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值