9 February 2022

Implementing Iterable<T>

We have the collections for loop that looks like this.


for(Type item: collection)
{
    do(item);
}

We can make this work for our Stack class. This is accomplished by implemeting the Iterable<T> interface. This interface has one required method,


public Iterator<T> iterator();

What's an iterator? This interface specifies the following methods.