請問我這個為啥會報越界異常呢
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("學(xué)生:"+student.name);
?? ??? ?}
?? ??? ?Collections.sort(studentlist);
?? ??? ?System.out.println("-------------排序后---------------------");
?? ??? ?for (Student student : studentlist) {
?? ??? ??? ?System.out.println("學(xué)生:"+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)
2019-09-23
循環(huán)中多了i++草看了半天