An Iterator is an object that allows you to visit each element of a Collection using a consistent interface for all types of collections. You can never instantiate an Iterator directly (in other words, you can't use the new operator to create an Iterator); instead, you create an Iterator by calling a Collection object's createIterator() method, which creates an iterator customized for that specific type of collection.
When you create an Iterator via a Collection's createIterator() method, the Iterator refers to a "snapshot" of the collection, and is initialized so that the first call to getNext() will return the first element of the collection. The Iterator uses a snapshot of the collection to ensure that changes made to the collection after creating the Iterator do not affect the iteration.
- For List objects, the elements of the list are returned in order of the index values; the first item returned is the item at index 1, the second is the item at index 2, and so forth.
- For LookupTable objects, the elements are returned in an arbitrary order that depends on the internal arrangement of the table's elements.
- For Vector objects, the elements are returned in order of index values.
Each time you call getNext(), the iterator updates its internal state to refer to the next element of the collection, so the next call will return the next item. When you first create an Iterator (by calling a Collection object's createIterator() method), the Iterator is initialized so that the first call to getNext() will return the first element of the collection.
After all of the collection's elements have been exhausted, calling getNext() will cause an error ("out of bounds") to be thrown.