how to fix java.lang.IndexOutOfBoundsException

后端 未结 7 1322
长发绾君心
长发绾君心 2020-12-10 23:58

Exception in thread \"main\" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:604)

相关标签:
7条回答
  • 2020-12-11 00:58
    for ( int i=0 ; i<=list.size() ; i++){
    ....}
    

    By executing this for loop , the loop will execute with a thrown exception as IndexOutOfBoundException cause, suppose list size is 10 , so when index i will get to 10 i.e when i=10 the exception will be thrown cause index=size, i.e. i=size and as known that Java considers index starting from 0,1,2...etc the expression which Java agrees upon is index < size. So the solution for such exception is to make the statement in loop as i<list.size()

    for ( int i=0 ; i<list.size() ; i++){
    ...}
    
    0 讨论(0)
提交回复
热议问题