亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

請問我這個為啥會報越界異常呢

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)


正在回答

1 回答

循環中多了i++草看了半天


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

請問我這個為啥會報越界異常呢

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號