IndexOutOfBoundsException when adding to ArrayList at index

前端 未结 10 2176
既然无缘
既然无缘 2020-12-17 16:39

I get exception Exception in thread \"main\" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 for the below code. But couldn\'t understand why.

         


        
10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 16:50

    By the way, ArrayList s = new ArrayList<>(10); set the initialCapacity to 10.

    Since the capacity of Arraylist is adjustable, it only makes the java knows the approximate capacity and try to avoid the performance loss caused by the capacity expansion.

提交回复
热议问题