Iterator接口方法在哪里有hasNext(),next(),remove()定义?因为接口永远不会定义方法只声明 [英] Where are Iterator interface methods hasNext(), next(), remove() defined? Because interfaces never define methods only declare

查看:340
本文介绍了Iterator接口方法在哪里有hasNext(),next(),remove()定义?因为接口永远不会定义方法只声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Iterator接口方法在哪里定义了hasNext(),next(),remove()等?接口不定义方法,只声明在其他类中定义的方法。我们如何通过Iterator引用直接访问hasnext(),next()等方法?
例如:

Where are Iterator interface method like hasNext(), next(), remove(), etc defined? Interfaces don't define methods, only declare methods that are defined in other classes. How can we directly access hasnext(), next(), etc methods through Iterator reference? For example:

List l = new LinkedList();
l.add(1);
l.add(2); 
Iterator iter = l.iterator();
while(iter.hasNext())
{
    Object o = iter.next();
    System.out.println(o);
}

在上面的示例中,使用引用iter(Iterator引用)可以指定任何方法在iter参考?请给我一个代码示例。直到现在我才理解这个概念。

In the above example using reference iter (Iterator reference) can we assign any method in iter reference? Please give me an example with code. I could not understand this concept until now.

推荐答案

如果您的问题是:LinkedList迭代器的实现在哪里,答案是:
它是作为LinkedList类的私有内部类实现的:

If your question is: where is the implementation of the LinkedList iterator, the answer is: it's implemented as a private inner class of the LinkedList class:

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

    ...
}

这篇关于Iterator接口方法在哪里有hasNext(),next(),remove()定义?因为接口永远不会定义方法只声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆