thanks for taking the time to read this i am working through the java.util class at the moment and was wondering the following....
Iterator is an Interface, as was/is Enumerator As such it contains only abstract methods which must be implemented Collections' methods .Iterator() are used to obtain an Iterator object If though the Iterator is an Interface where is the implementation of the methods performed?
Are there a hidden classes for each of the Collection types? ie.Iterator i=new ArrayListIterator() Thanks again simon
You are correct. The interator interface is a nested (inner) class implementation. Take a look at the LinkedList.java implementation. When you call LinkedList.iterator(), you'll get a reference to an object that implements the iterator interface. Specifically,
Why do you care that it's has to be a top-level class? As long as the class implements the iterator interface and does the correct thing, you should care less about the detail implementation. It does help to study the detail implementation. The reason it's a nested class, is because nested class are allowed full access to the LinkedList member instance variables which is a requirement for the iterator. -Peter