User:Szucsatti/Iterator

The need for a iterator patterns arises from the fact that we often need to go through a collection of objects that is represented differently. If we need to parse an ArrayList we use the it's get method called for every object in the List. On the other side, when using a simple array we simply apply the index of the element on the array. If there is a part in our code when we need to parse different collections (and we don't know the implementations) writing a for or a while for each type of collection that might come is definitely not a good idea. (The idea is to parse different collections with the same code).

We create a core interface. All the iterators have to implement this interface. After this step we create an iterator for all the collections that we need to use. When iterating though collections or lists we will always use the methods of this iterator interface.