简体   繁体   中英

java.lang.IndexOutOfBoundsException while removing from ArrayList?

I am trying to remove an object from an ArrayList, My code is

ArrayList myArrayList=new ArrayList();

for(int index=0;index<20;index++){
    myArrayList.add(index);
}

for(int removeIndex=0;removeIndex<=mArrayList;removeIndex++){
        myArrayList.remove(removeIndex);
}

It is giving a java.lang.IndexOutOfBoundsException . How do I remove a number of objects from ArrayList ?.

Of course, as soon as you remove the 0th item, the last item is now 18th, because the items are reindexed.

You can use several tricks, for example, remove starting from the end. Or remove the 0th item until the array is empty (or until you removed some predefined number of items).

Code:

for(int index = mArrayList.size() - 1; removeIndex >= 0; removeIndex--) {
    myArrayList.remove(removeIndex);
}

or

for(int nremoved = mArrayList.size() - 1; nremoved >= 0; nremoved--) {
    myArrayList.remove(0);
}

If you want to remove all the items, you can consider using clear() as well.

If you want to remove several positions from a list, you can try the following:

Collections.sort(positions); // needed if not already sorted
for (int i = positions.size() - 1; i >= 0; i--)
    myArrayList.remove(positions.get(i));

List#clear()将删除所有元素。

You're comparing your removeIndex to the ArrayList itself instead of to ArrayList.size() . Also, you should use smaller than ( < ) instead of smaller than or equal to ( <= ) in the comparison, because using < results in an extra loop which causes an indexOutOfBoundsException .

Furthermore, start removing at the end of the ArrayList instead of at the beginning to avoid re-indexing of the elements, which can also cause an indexOutOfBoundsException . (Not in this case since you're comparing to Array.size() every loop. Instead you're removing every second item, as Vlad also mentions.)

You have to check '<' while removing.

ArrayList myArrayList = new ArrayList();

        for(int index=0;index<20;index++){
            myArrayList.add(index);
        }

        for(int removeIndex=0;removeIndex<myArrayList.size();removeIndex++){
                myArrayList.remove(removeIndex);
        }

When you remove an element from ArrayList all its subsequent elements reduce their indeces by one.

Please refer to public ArrayList.remove(int index)

if you needed to remove all elements from Array, and if is possible, then better would be

myArrayList = new ArrayList();

and inside loop you have to reset Array this way, because clear() or removeAll() doesn't works

In most common way use Iterator instead:

final Iterator<? extends T> it = collection.iterator();
while ( it.hasNext() ) {
    T t = it.next();
    if (isNeedToRemove(t)) {
        it.remove();
    }
}

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