package Practice;
import java.util.LinkedList;
import java.util.Iterator;
public class CollectionTest {
public static void main(String[] args) {
LinkedList<String> c = new LinkedList<String>();
c.add("Hello");
c.add("World");
c.add("java");
Iterator<String> it = c.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
My question: Since Collection extends Iterable interface, so it has the abstract method iterator() which is defined in Iterable. So, LinkedList which is a concrete class that implements List interface that extends Collection interface, should have an overridden version of the iterator() method, I guess...
I saw the SUN company source codes for the ArrayList class, and inside there is an inner class "Itr" which implements Iterator interface, and by calling the "public Iterator iterator()" method inside ArrayList class, it will return a "new Itr()".
But my confusion is that I cannot find an iterator() method in the LinkedList. I only found a "descendingIterator()" method in LinkedList class which is not what I am looking for... But where exactly is the overridden version of the iterator() method in LinkedList? In my codes I created a LinkedList object, and used this object to invoke its iterator() method, and it works. But since there's no overridden version of interator() method in LinkedList, why can it work? Is it calling the interator() method in its super classes like List? Collection? But these are all interfaces with no concrete method body for interator().... Also why it still works when iterator reference "it" is calling the hasNext and next methods?
Sorry spare with my lengthy question statements... I hope this can make you understand my confusions.... Appreciate greatly for your help thank you!
public abstract class AbstractSequentialList<E> extends AbstractList<E>. In Eclipse you can right click the method and choose "Open Declaration" and it shows you where the method is.