Rakshi Rakshi - 3 months ago 17
Android Question

How to remove items from generic arraylist in Android (Java)

I have a generic arraylist of an object here I want to remove certain elements, The problem is when I iterate the list with for loop, I can't do a simple sequence of

remove()
's because the elements are shifted after each removal.

Thanks

Answer

Use Iterator to remove element

Like

Iterator itr = list.iterator();
String strElement = "";
while (itr.hasNext()) {
    strElement = (String) itr.next();
    if (strElement.equals("2")) {
        itr.remove();
    }
}