public interface Iterator<E> { E.next(); boolean hasNext(); }
I understand some of these lines but not others. I think I understand these two lines: void add(E x); boolean hasNext(); They are just methods that need to be overwritten in classes that use this interface.
But I don't understand this line in the List interface: iterator <E> iterator(); Does that mean that the method is called iterator(), but it returns something of type iterator? Why is it allowed to use the same name for the method name and the return type? That seems circular.
I also don't understand this line in the Iterator interface: E.next(); Where does the next() come from? Why doesn't it need to be defined anywhere?
Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal
It's not E.next(), it's E next(); it's a method named next() that returns a value of type E, the type parameter.
The iterator() method returns a value of type Iterator<E>. Case is significant in Java, so the method name and the name of the return type are not the same. But even if they were, it would be OK; class names like "Iterator" are not reserved words, and they don't interfere with method or variable names. The following code is perfectly legal (if stylistically awful):
"Foo" is used here for a class name, a method name, and a variable (parameter) name, all on the same line of code!