When the enhanced for loop (foreach loop) was added to Java, it was made to work with a target of either an array or Iterable
.
for ( T item : /*
I suggest to create a helper class with factory methods that you can use like this:
import static Iter.*;
for( Element i : iter(elements) ) {
}
for( Element i : iter(o, Element.class) ) {
}
As a next step, the return type of iter()
could be a fluent interface so you can do:
for( Element i : iter(elements).reverse() ) {
}
or maybe
for( Element i : reverse(elements) ) {
}
You should also have a look at op4j which solves many of these problems with a very nice API.