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;
private Scanner console;
public Student student ;
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(1, 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[] cour= {new Course("3","离散数学"),new Course("4","汇编语言")};
CoursesToSelect.addAll(Arrays.asList( cour));
Course temp3=(Course)CoursesToSelect.get(2);
Course temp4=(Course)CoursesToSelect.get(3);
// System.out.println("添加了两门课程:"+temp3.id+":"+temp3.name+temp4.id+":"+temp4.name);
Course[] cour2= {new Course("5","高等数学"),new Course("6","大学英语")};
CoursesToSelect.addAll(2,Arrays.asList(cour2));
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 SetTest() {
// CoursesToSelect=new ArrayList<Cour>();
// console=new Scanner(System.in);
// }
/**
* 测试List的Contains方法
* @param args
*/
public void testListContains() {
//取得备选课程序列的第0个元素
Course course=CoursesToSelect.get(0);
//打印输出couresToSelect是否包含course对象
System.out.println("取得课程:"+course.name);
System.out.println("备选课程中是否包含课程:"+course.name+","+CoursesToSelect.contains(course));
//提示输入课程名称
System.out.println("请输入课程名称:");
String name=console.next();
//创建一个新的课程对象,ID和名称,于course对象完全一样
Course course2=new Course();
course2.name=name;
System.out.println("新创建课程:"+course2.name);
System.out.println("备选课程中是否包含课程:"+course2.name+","+CoursesToSelect.contains(course2));
}
//创建学生对象并选课
public void creatStudentAndSelectCours() {
//创建一个学生对象
student=new Student("1","小明");
System.out.println("欢迎学生:"+student.name+"选课!");
//创建一个Scanner对象,用来接收从键盘输入的课程ID
console=new Scanner(System.in);
for(int i=0;i<3;i++){
System.out.println("请输入课程ID:");
String courId=console.next();
for(Course cr:CoursesToSelect) {
if(cr.id.equals(courId)) {
student.courses.add(cr);
}
}
}
}
/**
* 测试Set的contains方法
* @param args
*/
public void testSetContains() {
//提示输入课程名称
System.out.println("请输入学生已选的课程名称");
String name=console.next();
//创建一个新的课程对象,ID和名称,于course对象完全一样
Course course2=new Course();
course2.name=name;
System.out.println("新创建课程:"+course2.name);
System.out.println("备选课程中是否包含课程:"+course2.name+","+student.Cour.contains(course2));
}
public static void main(String[] args) {
SetTest st=new SetTest();
st.testAdd();
//st.testListContains();
st.testForEach();
st.creatStudentAndSelectCours();
st.testSetContains();
}
public void testForeachForSet(Student student) {
//打印除数,学生所选的课程!
for(Course cr:student.Cour) {
System.out.println("选择了课程:"+cr.id+cr.name);
}
}
}
Exception in thread "main" java.lang.NullPointerException
at com.imooc.collection.SetTest.testAdd(SetTest.java:17)
at com.imooc.collection.SetTest.main(SetTest.java:119)
解决了, ArrayList没有实例化,