The short version of for loop (T stands for my custom type):
for (T var : coll) {
//body of the loop
}
is translated into:
for (Iterator<T> iter = coll.iterator(); iter.hasNext(); ) {
T var = iter.next();
//body of the loop
}
and the Iterator for my collection might look like this:
class MyCollection<T> implements Iterable<T> {
public int size() { /*... */ }
public T get(int i) { /*... */ }
public Iterator<T> iterator() {
return new MyIterator();
}
class MyIterator implements Iterator<T> {
private int index = 0;
public boolean hasNext() {
return index < size();
}
public type next() {
return get(index++);
}
public void remove() {
throw new UnsupportedOperationException("not supported yet");
}
}
}