I am a new-comer in Java and in process of learning. I need a an answer of following question supported with valid theory. Consider the following line-
Iterat
You are right saying that Iterator
is an interface. So, some class must implement this interface so you can use it.
The iterator interface defines what an iterator should do. In this case, the class ArrayList provides its own implementation of this interface.
What you get from al.iterator()
is the inner class of ArrayList Itr (see following code, which IS iterator. (as we kind of describe the IS-A relationship)
(you can think of an inner class as a normal class for now. So the answer to your question is Itr, which is just a name, and all you should care about is its function, which is defined by interface Iterator.)
/**
* An optimized version of AbstractList.Itr
*/
private class Itr implements Iterator {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size;
}
.....
}
This code is by Josh Bloch and Neal Gafter, who wrote the ArrayList class.