Not if you do it correctly with myIterator.remove(), and the collection supports removal. Collections which don't support removal will throw a different exception.
Damon McNeill wrote:Generally you don't want to mutate a collection while you are iterating over it as you will get a ConcurrentModificationException. . . .
Please explain how filter() would work. Since filter() is usually used with a predicate on the element rather than its index, that is going to involve some very awkward programming.
. . . only the elements you want to keep. A "filter" operation, available using Java8 streams.
Agree; trying to remember indices like that will make for some very strange programming and will be very error‑prone, so your suggestion of copying elements to a new collection sounds much better.
. . . both the index i and the .length property in the for loop here will "desync" from the underlying array as elements are removed.
Campbell Ritchie wrote:. . . but in small towns here, farmers occasionally dump that second ton of “fertiliseer” outside the bank office. Maybe that is why the banks are closing their branches in so many small towns
Tim Holloway wrote:. . . spell "furt-uh-LIE-zah" . . . .