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
The implementation is defined in AbstractList
itself. Find it at its source code.
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
private class Itr implements Iterator<E> {
...
}
public Iterator<E> iterator() {
return new Itr();
}
}
Try
System.out.println(new ArrayList<String>().iterator().getClass().getName());
output
java.util.AbstractList$Itr
Iterator is generic and doesn't return a specific type unless you define it in the ArrayList. Iterator is an interface (part of the Java Collections) that returns the type that was passed to it. The Iterator is used to traverse the list of elements, and remove an element if necessary.
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<E> {
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.
I want to know what is here the return type of al.iterator()
You shouldn't care. It doesn't matter what particular implementation of Iterator the method returns, only that it returns an Iterator. You can call next, hasNext, and remove, or iterate with a for-each loop, without knowing that it happens to be an ArrayList.Itr or whatever the implementation is.
Suppose the Java developers working on the ArrayList want to rename their iterator implementation to ArrayListIterator instead of whatever they're calling it now. If your code relied on knowing the exact Iterator implementation, you would have to change your code just to handle an ArrayList-internal change you shouldn't even see.
You can define the return type of your Iterator beforehand. As you declare the iterator, you can (and should) define the type of your list items like this:
Iterator<File> itr = al.iterator();
So when you access itr
you don't have to cast to file, e.g. (File) itr.next()
but can directly use it in your while loop:
while (it.hasNext()) { File f = itr.next() }
Your compiler now won't complain when using next().
Simply it returns the object it does iterate. For example you can refer to the code below.
while(itr.hasNext())
{
String str = itr.next().toString(); // Converting an answer type object into String.
System.out.println(str);
}