aspose file tools
The moose likes Java in General and the fly likes definitions from the List and Iterator interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "definitions from the List and Iterator interfaces" Watch "definitions from the List and Iterator interfaces" New topic
Author

definitions from the List and Iterator interfaces

Jacob Collins
Greenhorn

Joined: Jun 14, 2006
Posts: 8
Here is an abbreviated excerpt from the definition of the interfaces List and Iterator in the package java.util:

public interface List<E> {
void add(E x);
iterator <E> iterator();
}

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

Joined: Jul 08, 2003
Posts: 24061
    
  13

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!


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: definitions from the List and Iterator interfaces
 
Similar Threads
A kinder, gentler API for reading lines
Collections question
Implement methods for Iterator using Generics for TreeSet
Iterator Pattern
Generics and ? symbol