Return type of iterator() method in java

前端 未结 6 1482
耶瑟儿~
耶瑟儿~ 2021-01-03 07:46

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         


        
相关标签:
6条回答
  • 2021-01-03 08:38

    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
    
    0 讨论(0)
  • 2021-01-03 08:44

    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.

    0 讨论(0)
  • 2021-01-03 08:46

    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.

    0 讨论(0)
  • 2021-01-03 08:48

    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.

    0 讨论(0)
  • 2021-01-03 08:49

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

    0 讨论(0)
  • 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);
        }
    
    0 讨论(0)
提交回复
热议问题