为什么每次运行后,在控制台结果的顺序都不一样。。。
是System.out.println;你试一下;不是System.err.println;
附上代码
package com.集合类; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.List; //备选课程 public class ListTest { //用于存放备选课程的List public List coursesToSelect; public ListTest(){ this.coursesToSelect = new ArrayList(); } //用于往coursesToSelect中添加备选课程 public void textAdd(){ //创建一个课程对象,并通过调用add方法,添加到备选课程List中 Course cr1 = new Course("1","数据结构"); coursesToSelect.add(cr1); Course temp = (Course) coursesToSelect.get(0); System.err.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 cr3 = new Course("3", "text"); //coursesToSelect(4,cr3); Course[] course = {new Course("3", "离散数学"),new Course("4", "汇编语言")}; coursesToSelect.addAll(Arrays.asList(course)); Course temp3 = (Course) coursesToSelect.get(2); Course temp4 = (Course) coursesToSelect.get(3); System.err.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.err.println("添加两个课程:"+temp5.id+":"+temp5.name+temp6.id+":"+temp6.name); } //取得List中的元素的方法 public void testGet(){ int size = coursesToSelect.size(); System.out.println("有如下课程待选:"); for(int i=0;i<size;i++){ Course cr = (Course) coursesToSelect.get(i); System.err.println("课程:"+cr.id+":"+cr.name); } } public static void main(String[] args) { ListTest lt = new ListTest(); lt.textAdd(); lt.testGet(); } }