请问我这个为啥会报越界异常呢
public void testSort3() {
List<Student> studentlist=new ArrayList<Student>();
Random rd=new Random();
int key;
List<Integer> keylist=new ArrayList<Integer>();
for(int i=0;i<3;i++) {
do{
key=rd.nextInt(10);
}while(keylist.contains(key));
keylist.add(key);
i++;
}
studentlist.add(new Student(keylist.get(0)+"","Mike"));
studentlist.add(new Student(keylist.get(1)+"","Angle"));
studentlist.add(new Student(keylist.get(2)+"","Lucy"));
System.out.println("------------排序前-----------------");
for (Student student : studentlist) {
System.out.println("学生:"+student.name);
}
Collections.sort(studentlist);
System.out.println("-------------排序后---------------------");
for (Student student : studentlist) {
System.out.println("学生:"+student.name);
}
}
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at imooc_collection.collectionsTest.testSort3(collectionsTest.java:114)
at imooc_collection.collectionsTest.main(collectionsTest.java:134)