list的get方法index输入的是要索引的字段,和得出的结果不对啊,索引应该没有写错啊
public void testadd() {
//创建一个课程对象,并通过调用add方法,添加到备选课程list中
Course cr1=new Course("1","数据结构");
coursetoselect.add(cr1);
Course temp=(Course) coursetoselect.get(0);
System.out.println("添加了课程"+temp.id+":"+temp.name );
Course cr2=new Course("2","C语言");
coursetoselect.add(0,cr2);
Course temp1=(Course)coursetoselect.get(0);
System.out.println("添加了课程"+temp1.id+":"+temp1.name );
coursetoselect.add(cr1);
Course temp0=(Course) coursetoselect.get(1);
System.out.println("添加了课程"+temp0.id+":"+temp0.name );
//以下方法会抛出数组下标越界异常
// Course cr3=new Course("3","test");
// coursetoselect.add(4,cr3);
Course[] course= {new Course("3","离散数学"),new Course("4","汇编语言")};
coursetoselect.addAll(Arrays.asList(course));
Course temp2=(Course) coursetoselect.get(2);
Course temp3=(Course) coursetoselect.get(3);
System.out.println("添加了两门课程"+temp2.id+":"+temp2.name+";"+temp3.id+":"+temp3.name);
Course[] course2= {new Course("5","高等数学"),new Course("6","大学英语")};
coursetoselect.addAll(2,Arrays.asList(course2));
Course temp4=(Course) coursetoselect.get(4);
Course temp5=(Course) coursetoselect.get(5);
System.out.println("添加了两门课程"+temp4.id+":"+temp4.name+";"+temp5.id+":"+temp5.name);
}
得出的结果
添加了课程1:数据结构
添加了课程2:C语言
添加了课程1:数据结构
添加了两门课程1:数据结构;3:离散数学
添加了两门课程1:数据结构;3:离散数学