They say that every problem in computer science can be solved in a more indirect way.
class Indexed<T>
int index;
T value;
static <T> Iterable<Indexed<T>> indexed(Iterable<T> iterable){ ... }
for(Indexed<Dog> idog : indexed(dogs))
print(idog.index);
print(idog.value);
In java 8, we probably want to cancel this control pattern as
forEach(dogs, (index, dog)->{
print(index);
print(dog);
});
static <T> void forEach(Iterable<T> collections, Acceptor<T> acceptor){...}
interface Acceptor<T>
void accept(int index, T value);
source
share