for (String s: stringArray) { // do something with s }Wouldn't it be nice if we could use this syntax using other data structures. To do so, we need to make a data structure iterable, which means it provides an iterator, which is a mechanism for accessing each element in our data.
Iterators are pretty useful when dealing with data structures because of the level of abstraction that they provide. So far we've been dealing with data structures that have been pretty linear in nature or conceptualiztion. Iterating through these haven't been difficult, the user can just use a for loop to iterate through them. This won't be easy as we learn more about complex data structures. We will need to provide a way for the user to iterate through them without being complicated. That's where Iterators come in.
Let's look at the example of making an array iterable.
You will need the following file:
An interesting thing to note about this class, is that the iterator class is an inner class: it is defined within the MyArrayList class, and therefore also has access to its instance variables.
Your second task for this recitation is to complete the implementation of RecursiveLinkedList.java.