How to remove everything from an ArrayList in Java but the first element

后端 未结 11 1419
一生所求
一生所求 2020-12-17 09:39

I am new to java programming, been programing in php so I\'m used to this type of loop:

int size = mapOverlays.size();
for(int n=1;n

        
相关标签:
11条回答
  • 2020-12-17 09:42

    Simple.

    mapOverlays = Collections.singletonList(mapOverlays.get(0));
    
    0 讨论(0)
  • 2020-12-17 09:44

    I'm assuming that mapOverlays holds an ArrayList reference.

    If mapOverlays is declared as a List or ArrayList, then mapOverlays.remove(n) will refer to the remove(int) method that removes the object at a given offset. (So far so good ...)

    When you remove the nth element of an array using remove(int), the elements starting at position n + 1 and above all get moved down by one. So what you are doing won't actually work in most cases. (In fact, you are likely to remove about half of the elements you want to remove, and then get an IndexOutOfBoundsException.)

    The best solution is either:

        for (int i = size - 1; i > 0; i--) {
            mapOverlays.remove(i);
        }
    

    or

        tmp = mapOverlays.remove(0);
        mapOverlays.clear();
        mapOverlays.add(tmp);
    

    (Note that the first solution always removes from the end of the list, avoiding the need to copy elements to fill in the hole left by the removed element. The performance different is significant for a large ArrayList.)

    However, if mapOverlays is declared as a Collection, remove(n) will bind to the remove(<E>) overload which removes the object that matches its argument. Depending on the declared type, this will either give you a compilation error, or the int will be autoboxed as an Integer and you (probably) won't remove anything. GOTCHA!

    0 讨论(0)
  • 2020-12-17 09:45

    Use a while loop to remove everything after the first element:

    while (mapOverlays.size() > 1) {
        mapOverlays.remove(1);
    }
    

    EDIT (see comment from Adam Crume)

    If performance is a problem you should use this one

    while (mapOverlays.size() > 1) {
        mapOverlays.remove(mapOverlays.size()-1);
    }
    

    even a bit micro-optimization

    int last = mapOverlays.size() - 1;
    while (last >= 1) {
        mapOverlays.remove(last);
        last -= 1;
    }
    



    If performance is really a problem (and the list has lots of elements), you should use the sublist solution. It's a bit harder to read but probably the fastest solution if the list instance can not be recreated (referenced elsewhere).

    0 讨论(0)
  • 2020-12-17 09:47

    Use google collections and Lists.limit http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Iterables.html#limit(java.lang.Iterable, int)

    0 讨论(0)
  • 2020-12-17 09:59

    An ArrayList has integer indices from 0 to size() - 1. You could do:

    int size = mapOverlays.size();
    for(int n=1;n<size;n++)
    {
        mapOverlays.remove(1);
    }
    

    This probably matches what you expect from PHP. It works by continually removing the 1th element, which changes. However, this has poor performance, since the internal array must constantly be shifted down. It is better to use clear() or go in reverse order.

    It's too bad removeRange is protected, as that would be convenient for this type of operation.

    0 讨论(0)
  • 2020-12-17 10:01

    You could use

    mapOverlays.subList(1, mapOverlays.size()).clear();
    
    0 讨论(0)
提交回复
热议问题