Looping through and arraylist and removing elements at specified index

前端 未结 6 1479
栀梦
栀梦 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:09

    it is normal that only half the list is empty, because when you remove elements from you list the size of the list decreases, but the index increases. And they meet up in the middle. If you really would like to remove elements one by one, and remove from the last to the first element. I suggest you use:

    while (!al.isEmpty())
    {
    
        al.remove(al.indexOf(al.size()-1));
    
    }
    
    0 讨论(0)
  • 2020-12-21 04:14

    Look through you loop where you removing elements again. Every time you remove and element the value returned by al.size() reduces, while you index increases. That means you will only be iterating half the time you want to.

    The fix would be to do this.

    int size = al.size();
    for(int index = 0; index < size; index++ ) {
    

    then do work. this way the size does not change.

    The other thing to remember is that when you remove something from the arraylist at index 0, index 1 will become index 0. So it might be better to iterate from

    int index = al.size(); index >=0 ; index--
    
    0 讨论(0)
  • 2020-12-21 04:18

    This happens because you are altering the indexes by removing. If you remove element 0, element 1 now becomes element 0. Now when you next remove 1, that is what used to be element 2 and what was element 1 still exists at index 0.

    The easiest way to avoid this is to loop backwards from end to beginning.

    alternatively, you could just keep removing index 0 until the ArrayList is empty.

    0 讨论(0)
  • 2020-12-21 04:20

    When removing an item the list it contracts.

    Let's say you have item 0 1 and 2

    Your index is 0

    You remove 0, your index is now 1, you remove 2 cause that's now at index 1.

    Makes sense ?

    0 讨论(0)
  • 2020-12-21 04:22

    Be aware that you can remove all the elements at once by simply using the clear() method. In your code, the problem is that the list is being modified at the same time that you're iterating over it, effectively decreasing its size, so the index < al.size() condition fails. Try this instead:

    for (int index = 0, n = al.size(); index < n; index++)
        al.remove(0);
    

    Alternatively, this solution removes elements at the end, making it more efficient (it's no longer necessary to copy elements around):

    for (int idx = al.size() - 1; idx >= 0; idx--)
        al.remove(idx);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题