Interfaces Iterator and Iterable
Suppose b is a String array, or an object of class java.util.ArrayList<String>, or of java.util.Set<String>. Then, one can write a foreach loop that processes each element of b like this:
for (String s : b) {
Process s
}
With each iteration of the loop, another element of b is stored in s and then "processed" —whatever that means.
The use of such a foreach loop is made possible by the use of two interfaces: Iterator and Iterable. That's what we discuss in this module. You can learn about them by watching less than 15 minutes of videos, given below.
Interface Iterator
Alex Fusco tells you what we mean by an enumeration. He then introduces you to interface Iterator, describing its four methods and writing a while-loop to enumerate its values.
Note that we generally use only functions hasNext() and next(). (3.3 minutes) Read it here: iterator.pdf
An Iterator to enumerate the even values in an Integer array
We develop a class that implements interface Iterator. An object of the class can be used to enumerate the even values of an Integer array. This class can be used as a template for writing classes that implement iterators. (3.5 minutes) Read it here: evenValues.pdf
Be careful! Once you have an Iterator for some collection. it's best not to change that collection until the Iterator has been used to enumerate all elements. Changing the list at any time before that is quite likely to mess up the Iterator.
A simple implementation of a stack
Alex Fusco writes a class Stack that implements a stack in an array. The maximum size of the stack is the size of the array. This simple class will be used in the next video. (1.5 minutes) Read it here: classStack.pdf
At this point, we ask you to complete a class that provides an Iterator over the elements of an ArrayList in a rather strange order, from outside in. We give you a class OutsideIn to complete, as well as a completed class OutsideInTest. The comment at the top of OutsideIn.java explains everything. Start a new project in Eclipse for this exercise, put the two class in it, and program and debug. Have fun! You can get our solution at the end of this file. OutsideIn.zip
An iterator as an inner class
Alex Fusco develops an iterator for class Stack of the previous video. The iterator enumerates the elements of a stack from the top to the bottom. The interesting point is that the iterator is a private inner class of Stack and that a public method is used to create and return an object of the inner class. (3 minutes) Read it here: iteratorForStack.pdf
Making class stack "Iterable"
It is now easy to fix class Stack so that we can use a foreach loop to enumerate its elements.
Just have it implement interface java.lang.Iterable. (1.5 minutes) Read it here: Iterable.pdf
Here is the code Alex developed. Put it into Eclipse: DemoStackIterator.zip
Please note that there is a typo near the end of this video.
Near the end of the video, an example is given of the use of a foreach statement.
The line that creates the stack should use type String, not Integer: Stack<String> st= new Stack<String>(50); .
Solution to the OutsideIn problem: outsideInSolution.zip