当前位置: 首页 > 知识库问答 >
问题:

什么类是直接实现ListIterator?

阎祖鹤
2023-03-14

什么类实际实现了接口ListIterator?我正在研究收集API。。而且似乎在列表实现类中返回列表迭代器的实际方法是直接在列表本身中指定的。

ListIterator接口指定用于操作迭代器的实际方法。。但是我很难找到实现这个的实际类。

哪个类实际上-实现-接口ListIterator,允许List类中的方法工作?

我确信必须有一个实现类,因为调用listIterator()本身肯定不能返回接口,必须有实例化的东西。

共有3个答案

呼延承平
2023-03-14

ListIterator添加了迭代列表API的功能。这是一个使用示例:

列表

公孙驰
2023-03-14

您不能实例化接口,但始终可以创建“不可见的”私有类,这样可以满足接口,但用户无法直接看到它。例如,LinkedListlistIterator定义为:

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;

    ListItr(int index) {
        // assert isPositionIndex(index);
        next = (index == size) ? null : node(index);
        nextIndex = index;
    }

    // much more
}

这种行为在lambdas中有它的最终形式,在这种形式中,您不会有任何寻找所传递类的源的诱惑。

百里成仁
2023-03-14

使用源代码luke。。。使用源代码

来自JDK 8(1.8.0_45)中的ArrayList的源代码

public ListIterator<E> listIterator() {
    return new ListItr(0);
}

// then line 905
/**
 * An optimized version of AbstractList.ListItr
 */
private class ListItr extends Itr implements ListIterator<E> {
    ListItr(int index) {
        super();
        cursor = index;
    }
// etc

因此,ArrayList返回的ListIterator是实现接口ListIterator的私有内部类。

 类似资料:
  • 问题内容: @Entity public class Husband implements Serializable { 广义上是什么? 类为什么实现接口? 为什么丈夫成员一个人只有@OnetoOne(mappedBy =“ Wife”),而妻子成员却没有@OnetoOne(mappedBy =“ husband”) 问题答案: 广义上讲,序列化是Java为开发人员提供的将任何对象的状态持久保存到

  • 这句话刚从我脑海中浮现出来: 我知道Queue是一个接口,LinkedList类“实现”Queue拥有的所有方法(

  • 我知道这是一个很基本的问题,但一个面试官用一种很诡计的方式问我,我很无奈:( 我只知道一个界面的材料或理论定义,并在我工作的许多项目中实现了它。但我真的不明白这为什么有用,怎么有用。 接口方面有一点我也不懂。例如,我们使用 finally块中的。但我没有看到该类实现或继承接口()类,我的意思是。我想知道怎么才能直接调用方法名。同样,我不理解Dispose方法是如何工作的,因为我们需要用我们自己的实

  • 我正在学习一本书,即“.NET域驱动的C#设计”。 问题基于如下类图所示的场景: 图:http://screencast.com/t/a9uuljvw0 现在,如果我用ICompanyRepository变量comrep调用Add()函数... 然后调用RepositoryBase类(它是CompanyRepository的父类)中的Add()函数。 我的问题是:在(抽象基)类“repositor

  • 为什么这段代码中不允许函数?

  • Monoid函数的类型签名是: