Looping through and arraylist and removing elements at specified index

前端 未结 6 1504
栀梦
栀梦 2020-12-21 04:02

I was trying an exercise where I would add 1000 elements to an arraylist and then remove them systematically from the list again(by specifying the index). The idea behind th

6条回答
  •  没有蜡笔的小新
    2020-12-21 04:31

    Because while you are removing elements from the ArrayList its index is being updated. So you remove an element at position 0 and the element in position 1 is in the index 0 position now. So when you delete element at index 1 you are rally removing element at index 2 of the original ArrayList, and so on.

提交回复
热议问题