Python | Java |
By convention, the iter() method returns an iterator on an iterable object. The user of an iterator can expect the method next() to return the next object in an iteration, until next raises a StopIteration exception. At that point, one closes the iterator using the close() method.
Let us assume that the LinkedStack class now includes an itermethod. Then one can visit the objects in a stack, from top to bottom, in either of the following ways: stack = LinkedStack() The iter method actually builds and returns a generator object. The code for this object executes in a separate process running concurrently with the process that uses the iterator. A generator object can maintain state, such as a current position pointer to the elements in the collection. This reference in the current example is initially to the first node in the stack’s linked list. The generator’s code also executes a while True loop. If the current position equals None, then the last node has been passed and the generator raises a StopIteration exception. Otherwise, the generator yields the element at the current node. The yield statement pauses the execution of the process executing the generator’s code until the method next() is called. This method returns the element just yielded. When control returns to the generator object, the current pointer is set to the next field of the current node. The generator’s process runs forever, unless the user calls its close() method. Example: class OneWayNode: def __init__(self, data, next): class LinkedStack: def __init__(self): def push(self, element): def pop(self): def peek(self): def isEmpty(self): def __len__(self): def __iter__(self): |
The iterator() method returns an iterator on an iterable object. The user of an iterator can expect the method next() to return the next object in an iteration, while the method hasNext() returns True.
Let us assume that the LinkedStack class now includes an iterator method. Then one can visit the objects in a stack, from top to bottom, in either of the following ways: TrueStack<String> stack = new LinedStack<String>(); for (String element : stack) The implementing class defines an iterator() method that returns an instance of an inner class. This class implements the Iterator interface. Its methods next() and hasNext() track a current position pointer to the elements in the collection. Note that several iterators may be open concurrently on the same collection. To maintain the consistency of each iterator with the collection’s data, collection-based modifications (push, pop) are not allowed during the operation of any iterator. The collection now maintains a count of its modifications. When an iterator is instantiated, it sets its own count of modifications to the collection’s count. On each call of the next() method, the iterator compares the two counts. If they are not the same, a collection-based modification has occurred and an exception is thrown. Example: import java.util.iterator; public class LinkedStack<E> implements TrueStack<E>{ private OneWayNode<E> items; public LinkedStack(){ public void push(E element){ public E peek(){ public boolean isEmpty(){ public int size(){ public Iterator<E> iterator(){ private class StackIterator<E> implements Iterator<E>{ private OneWayNode curPos; private StackIterator(){ public boolean hasNext(){ public E next(){ public void remove(){ private class OneWayNode<E>{ private E data; private OneWayNode(E data, OneWayNode next){ |