package com.XuanKe;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
/**
* 备选课
* @author Administrator
*
*/
public class BeiXUanKeTest {
private List CourseToSelect;
public List getCourseToSelect() {
return CourseToSelect;
}
public void setCourseToSelect(List courseToSelect) {
this.CourseToSelect = new ArrayList();
}
@SuppressWarnings("unchecked")
public void CourseAdd(){//添加备选课程
Course cr1=new Course();
cr1.setId("1");
cr1.setName("数据结构");
CourseToSelect.add(cr1);
Course temp =(Course) CourseToSelect.get(0);
System.out.println("添加课程为:"+temp.getId()+" :"+temp.getName());
Course cr2=new Course();
cr2.setId("2");
cr2.setName("C语言");
CourseToSelect.add(0, cr2);
Course temp2=(Course) CourseToSelect.get(1);
System.out.println("添加课程为:"+temp2.getId()+" :"+temp2.getName());
//数组添加课程
Course cr3=new Course();
cr3.setId("3");
cr3.setName("离散数学");
Course cr4=new Course();
cr4.setId("4");
cr4.setName("汇编语言");
Course[] course={cr3,cr4};
CourseToSelect.addAll(Arrays.asList(course));
Course temp3 =(Course) CourseToSelect.get(2);
Course temp4 =(Course) CourseToSelect.get(3);
System.out.println("添加课程为:"+temp3.getId()+" "+temp3.getName()+" "+temp4.getId()+" "+temp4.getName());
Course cr5=new Course();
cr5.setId("5");
cr5.setName("高等数学");
Course cr6=new Course();
cr6.setId("6");
cr6.setName("大学英语");
Course[] course1={cr5,cr6};
CourseToSelect.addAll(2,Arrays.asList(course1));
Course temp5 =(Course) CourseToSelect.get(2);
Course temp6 =(Course) CourseToSelect.get(3);
System.out.println("添加课程为:"+temp5.getId()+" "+temp5.getName()+" "+temp6.getId()+" "+temp6.getName());
}
//输出备选课程
public void getBeiXuan(){
//用数组
System.out.println("有如下备选课:");
for(int i=0;i<CourseToSelect.size();i++){
CourseToSelect.get(i);
Course cr=(Course) CourseToSelect.get(i);
System.out.println("课程:"+cr.getId()+":"+cr.getName());
}
//用迭代器
System.out.println("有如下备选课(迭代器):");
Iterator it=CourseToSelect.iterator();
while(it.hasNext()){
Course cr=(Course) it.next();
System.out.println("课程:"+cr.getId()+":"+cr.getName());
}
//foreaach
System.out.println("有如下备选课(foreach):");
for(Object obj:CourseToSelect){
Course cr=(Course) obj;
System.out.println("课程"+cr.getId()+" :"+cr.getName());
}
}
public static void main(String[] args) {
BeiXUanKeTest bx=new BeiXUanKeTest();
bx.CourseAdd();
bx.getBeiXuan();
}
}
package com.XuanKe;
import java.util.HashSet;
import java.util.Set;
/**
* 学生类
* @author Administrator
*
*/
public class Student {
private String id;
private String name;
private Set course;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getCourse() {
return course;
}
public void setCourse(Set course) {
this.course = new HashSet();
}
}
package com.XuanKe;
/**
* 课程类
* @author Administrator
*
*/
public class Course {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我觉得首先空指针异常是你创建一个对象,没有进行实例化就使用的话就会出现空指针异常,你定义了List接口courseToSelect,虽然定义了set方法去实现接口,但是你使用courseToSelect时并没有调用set方法,也就是说CourseToSelect并没有实现。应该是吧,希望对你有所帮助!!!!
共同进步!