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

后端 未结 11 1468
一生所求
一生所求 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:59

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

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

    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.

提交回复
热议问题