Return type of iterator() method in java

北城余情 提交于 2019-12-18 17:28:23

问题


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-

Iterator itr = al.iterator(); 

where al is some collection object of ArrayList (class) type. I want to know what is here the return type of

al.iterator()

It is not definitely of a primitive data type, then it could be an object , but since every object belong to a class then it is of which class. Documentation and books etc says it has return type of Iterator. But Iterator is an interface. On other hand we say an interface could not have a direct object. Although interface variable could refer to an object of a class or classes that implements it.

-

So the above syntax is correct (as Iterator variable itr could be used to refer to an object of some class implementing it). But in realty it is object of which class? And can substituting itr with reference variable of that class will cause no error (I have tried substituting itr with ref. variable of ArrayList class in above line but that causes an error). I am using this syntax very often also in Generics form, but dont know the theory behind this. And I am supposing I am lacking a very basic concept here. Please rectify.


回答1:


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.




回答2:


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.




回答3:


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



回答4:


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().




回答5:


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.




回答6:


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);
    }


来源:https://stackoverflow.com/questions/23038578/return-type-of-iterator-method-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!