1

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

7 Answers 7

7

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));
Sign up to request clarification or add additional context in comments.

4 Comments

Hi vlad thks for reply ,exactly my question is how to remove one by one from arraylist(with out using clear())
it is working fine thks.......for suppose i want to remove particular positions like [0,3,7,11,19] here i taken one more arrayList for those position
my prob is,i have one ListView,my ListView contains check box by default all check boxes checked if we uncheck any check box the position added to mCheckedArrayList.i want remove all uncheck positions from Listview .
@sureshpati: if you want to remove several particular positions, I would just remove starting from the biggest index. This way you ensure that the indices won't change too early.
2

List#clear() will remove all elements.

2 Comments

Hi john thks for reply ,exactly my question is how to remove one by one from arraylist(with out using clear())
while(list is not empty) then remove the 0th index or remove from last index. Follow Vlad's advice.
0

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);
        }

Comments

0

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

Please refer to public ArrayList.remove(int index)

Comments

0

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

Comments

0

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

Comments

0

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();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.