class ArrayStack<T> implements Iterable<T>
{
// ...
// various methods we have
Iterator<T> iterator()
{
// will take snapshot approach
MyNiftyArrayStackIterator<T> mnasi= new MyNiftyArrayStackIterator<T>(a.clone(), size);
return mnasi;
}
}
class MyNiftyArrayStackIterator<T> implements Iterator<T>
{
int currElem = 0;
MyNiftyArrayStackIterator(T b[], int size) { save to local vars; }
boolean hasNext() { return currElem < size; }
T next() { return b[currElem++]; }
}
No comments:
Post a Comment