Java iterators and for-each-loop. any way to access the underlying iterator?

No, the whole point of the for-each-loop is to abstract away the underlying iterator If you need it, you must declare it.

No, the whole point of the for-each-loop is to abstract away the underlying iterator. If you need it, you must declare it.

1 And when you declare it, it should be a ListIterator if you're planning on removing items from the list. – Jack Cox Oct 15 '10 at 12:52.

No, you cannot remove objects in a for each loop. Use this instead: Iterator it = collection.iterator(); while (it.hasNext()) { if (it.next().shouldBeRemoved()) { it.remove(); } }.

2 I general prefer the for idiom rather than the while idiom because it keeps the scope of the iterator smaller. – dty Oct 15 '10 at 12:26 1 @dty: there is a valid point in your desire to keep all variables scope as narrow as possible. But as for me, code readability is on the first place.My version could be read almost as normal sentence: 'while iterator has next element do the following: ...', and yours seems very cumbersome.

So, if we want to keep benefits from both approaches, it's worth to extract mine code to a separate method. That's the best we can do with this problem, IMHO. – Roman Oct 15 '10 at 16:35.

If the collection is reasonably small, you can alternatively use a copy of the collection to iterate if you want to be able to remove elements so you won't have to act as if you have two collections. For(T e : collection.clone()) if(e.shouldBeRemoved()) collection.remove(); Even better, Apache CollectionUtils (and there is probably a Google alternative and a generics alternative) provides filter(java.util. Collection collection, Predicate predicate).

This example returns the whole list. You can store a predicate for reuse. CollectionUtils.

Filter(collection, new Predicate(){ boolean evaluate(Object object){return true;} }).

You are correct, using an Iterator supports the ability to remove an object from a source collection safely, by calling remove() on the Iterator itself. The point here is to avoid a ConcurrentModifiedException which implies that a collection was modified while an Iterator was open against it. Some collections will let you get away with removing or adding elements to a Collection while iterating across it, but calling remove() on the Iterator is a safer practice.

But Iterator supports a derived and more powerful cousin ListIterator, only available from Lists, supports both adding and removing from a List during iteration, as well as bidirectional scrolling through Lists.

Modifying data has a wrong feel to it, Just create a new Iterable(or collection) and add the items you want to keep to it, the other times are lost in the for loop ((kinda.

1 -1 Your wrong feel has a wrong feel to it. – Erick Robertson Oct 15 '10 at 12:37.

Even better, Apache CollectionUtils (and there is probably a Google alternative and a generics alternative) provides filter(java.util. Collection collection, Predicate predicate). This example returns the whole list.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions