发现我插入数组之后,只有保存数组最后一个元素的值,无论如何修改代码,修改插入,选择还是对象的数值打印的值还是一样
package imooc.com.collect;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ListSet {
//用于存放备选课程List
public List ListToSelect;
public ListSet(){
this.ListToSelect=new ArrayList();
}
//创建一个向List中添加课程的方法
public void ListAdd(){
Course s1=new Course("1","java程序设计");
ListToSelect.add(s1);
Course temp=(Course) ListToSelect.get(0);
System.out.println("所选课程为:"+temp.id+" "+temp.name); //这里必须对象实例化才能够调用
Course s2=new Course("2","C语言");
ListToSelect.add(1,s2);
Course temp2=(Course) ListToSelect.get(1);
System.out.println("所选课程为:"+temp2.id+" "+temp2.name);
Course[] s3={new Course("3","汇编语言"),new Course("4","单片机")};
ListToSelect.addAll(Arrays.asList(s3));
Course temp3=(Course) ListToSelect.get(2);
System.out.println("添加了两门课程"+temp3.id+":"+temp3.name+"\n");
Course temp4=(Course )ListToSelect.get(3);
System.out.println("添加了两门课程"+temp4.id+":"+temp4.name);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ListSet first=new ListSet();
first.ListAdd();
}
}