0

I have array of objects how to iterate throw this array and delete some objects depending on the condition:

ArrayList<Book>b=new ArrayList<Book>();
for(int i = 0 ; i < b.size ; i ++){
//if some condition
//remove b.get(i);
}

what should I do??

2
Contributors
7
Replies
16
Views
3 Years
Discussion Span
Last Post by holo12
0
  1. You can use b.remove(i) to remove entries. Loop down from the end of the list to the beginning to avoid confusing your loop's indexing when you delete an entry

  2. You can't use any ordinary iterator because they don't allow you to change the List while iterating.
    But...
    a ListIterator allows you to update & delete while iterating. You get one simply by b.listIterator() then use it as described in the API doc.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.