You can try the following:
List<Whatever> l = new ArrayList<Whatever>(); l.add(someStuff); Iterator<Whatever> it = l.iterator(); int i = 0; while (i < 2 && it.hasNext()) { it.next(); it.remove(); i++; }
Or more generally:
List<Whatever> l = new ArrayList<Whatever>(); l.add(someStuff); Iterator<Whatever> it = l.iterator(); while (it.hasNext()) { Whatever next = it.next(); if (shouldRemove(next)) { it.remove(); } }
EDIT: I think it depends on whether you are trying to delete specific indexes or specific objects. It also depends on how much logic you need to decide if something needs to be removed. If you know the indexes, then delete them in reverse order. If you have a set of objects that you want to remove, use removeAll. If you want to iterate over the list and remove objects matching the predicate, use the code above.
Cameron skinner
source share