简体   繁体   中英

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

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<size;n++)
{
    mapOverlays.remove(n);
}

So I want to remove everything but the first item, so why doesn't this work? As I get it, after removal, array keys are rearranged or not?

你可以用

mapOverlays.subList(1, mapOverlays.size()).clear();

As I get it, after removal, array keys are rearranged or not? Yes, the item which was on position 2 is on position 1 after you removed the item on position 1.

You can try this:

Object obj = mapOverlays.get(0); // remember first item
mapOverlays.clear(); // clear complete list
mapOverlays.add(obj); // add first item

Why don't you try backwards?

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

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.

I think it would be faster to create a new ArrayList with just the first element inside. something like :

E temp = mapOverlays.get(0);
mapOverlays = new ArrayList<E>().add(temp);

简单的。

mapOverlays = Collections.singletonList(mapOverlays.get(0));

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!

If you are using a java.util.List implementation instead of array, the size of the array gets smaller everytime you remove something and the n+1 item replaces the n item. This code will eventually result to ArrayIndecOutOfBoundsException when n becomes greated than the last index in the list.

Java also has an array type and the size of that one cannot be changed:

Object[] mapOverlay = //initialize the array here
int size = mapOverlay.length;
for(int n=1;n<size;n++)
{
    mapOverlay[n] = null;
}

I don't know PHP but this sounds like it's close to the behavior you are after. However the List implementations are more flexible and comfortable in use than the arrays.

EDIT: Here's a link to the Javadoc of List.remove(int) : http://java.sun.com/javase/6/docs/api/java/util/List.html#remove%28int%29

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

In java, if mapOverlays is list then it start with 0 as a first index.So n=0 in for loop.

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).

because in an ArrayList the first element has the index 0
in Java the correct way is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM