为什么我程序输出不是无序的??
package imooc.cn;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Settest {
public List<Course>courseToselect;
public Settest(){
courseToselect= new ArrayList<Course>();
}
public void testadd(){
/*用于往coursetoselect中添加备选课程*/
Course c1=new Course("1","数据结构");
courseToselect.add(c1);
Course temp=(Course)courseToselect.get(0);
// System.out.println("添加了课程:"+temp.id+":"+temp.name);
Course c2=new Course("2","C语言");
courseToselect.add(0, c2);
Course temp2=(Course)courseToselect.get(0);
// System.out.println("添加了课程:"+temp2.id+":"+temp2.name);
//以下方法会抛出数组下标越界异常
Course[] course={new Course("3","离散数学"), new Course("4","汇编语言")};
courseToselect.addAll(Arrays.asList(course));
Course temp3=(Course)courseToselect.get(2);
Course temp4=(Course)courseToselect.get(3);
//System.out.println("添加了两门课程:"+temp3.id+":"+temp3.name+";"+temp4.id+":"+temp4.name);
Course[] course2={new Course("5","高等数学"),new Course("6","大学英语")};
courseToselect.addAll(2,Arrays.asList(course2));
Course temp5=(Course)courseToselect.get(2);
Course temp6=(Course)courseToselect.get(3);
//System.out.println("添加了两门课程:"+temp5.id+":"+temp5.name+";"+temp6.id+":"+temp6.name);
}
public void foreach(){
System.out.println("foreach方法访问:");
for(Object obj:courseToselect){
Course c=(Course)obj;
System.out.println("课程:"+c.id+":"+c.name);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Settest st=new Settest();
st.testadd();
st.foreach();
//创建学生
Student xuesheng= new Student("1","小明");
System.out.println("欢迎"+xuesheng.name+"同学选课");
Scanner console=new Scanner(System.in);
for(int i=0;i<3;i++){
System.out.println("请输入课程id:");
String courseid=console.next();
for(Course c:st.courseToselect){
if(c.id.equals(courseid)){
xuesheng.courses.add(c);
}
}
}
System.out.println("共有多少门课程:"+xuesheng.courses.size());
//打印输出学生所选课程!
st.foreachset(xuesheng);
}
public void foreachset(Student xuesheng){
for (Course c : xuesheng.courses) {
System.out.println("选择了课程"+c.id+":"+c.name);
}
}
}