本文介绍了LinkedList.iterator()返回什么对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下对象:

Iterator<String> j = LinkedList.iterator();

看看Java文档,对于LinkedList类,LinkedList类中没有迭代器方法的实现,但是,该实现在AbstractSequentialList类中.

Looking at java docs, for LinkedList class there is no implementation of iterator method in LinkedList class, however, the implementation is in AbstractSequentialList class.

public Iterator<E> iterator() {
    return listIterator();

listIterator()方法在AbstractList类中实现,AbstractList类是AbstractSequentialList的父类,归纳起来,它返回一个迭代器对象,如果我没有记错的话,该迭代器对象不使用节点的概念.

listIterator() method is implemented in AbstractList class which is parent class for AbstractSequentialList and to sum it up it returns an iterator object which does not use concept of nodes if I'm not mistaken.

   public ListIterator<E> listIterator() {
    return listIterator(0);
}
private class ListItr extends Itr implements ListIterator<E> {
    ListItr(int index) {
        cursor = index;
    }

但是listIterator(int index)方法是在LinkedList类中实现的,并使用节点的概念.

But listIterator(int index) method IS implemented in LinkedList class and uses concept of nodes.

 public ListIterator<E> listIterator(int index) {
    checkPositionIndex(index);
    return new ListItr(index);
}

private class ListItr implements ListIterator<E> {
    private Node<E> lastReturned;
    private Node<E> next;
    private int nextIndex;
    private int expectedModCount = modCount;

那么回到j,是根"类AbstractList的迭代器还是使用实现LinkedList类?

so returning to j, is it an iterator from "root" class AbstractList or the implementation LinkedList class is used?

推荐答案

好吧,由于iterator()返回listIterator()listIterator()返回listIterator(0),并且listIterator(int index)LinkedListLinkedList覆盖的iterator()方法返回LinkedList$ListItr的实例.

Well, since iterator() returns listIterator(), listIterator() returns listIterator(0), and listIterator(int index) is overridden by LinkedList, LinkedList's iterator() method returns an instance of LinkedList$ListItr.

这篇关于LinkedList.iterator()返回什么对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 21:23