简体   繁体   English

关于迭代器接口的问题

[英]Questions about Iterator Interface

i'm studyng the basic interface and classes of the Java.util package.我正在研究 Java.util 包的基本接口和类。 Now the interface Set is a subclass of Collection and Iterable, so Set have the Iterator moudle.现在接口 Set 是 Collection 和 Iterable 的子类,所以 Set 有 Iterator 模块。 So why i use a reference of the Iterator Interface?那么为什么我使用迭代器接口的引用呢?

    Iterator<String> iterx = x.iterator();

Iteratr is also a interface. Iteratr 也是一个接口。 But i haven't see any relation about the Iterable interface and the Iterator interface.但是我没有看到有关 Iterable 接口和 Iterator 接口的任何关系。 Iterable have a moudle that is named Iterator. Iterable 有一个名为 Iterator 的模块。 it would not be more correct to write写起来不会更正确

Iterable.Iterator iterx = x.iterator();

Because x.iterator call the moudle that Set have inherited by Iterable, and don't call the Iterator interface.因为 x.iterator 调用的是 Iterable 继承的 Set 的 moudle,而不是调用 Iterator 接口。

What i don't have understand?我有什么不明白的?

The Iterable interface says that a particular Collection is iterable, which means you can iterate over it by using Iterator. Iterable 接口表示特定的 Collection 是可迭代的,这意味着您可以使用 Iterator 对其进行迭代。

It has only one method, which is iterator() returns an Iterator.它只有一种方法,即 iterator() 返回一个 Iterator。 Any collection eg List which implements Iterable provides implementation of iterator() method.任何集合,例如实现 Iterable 的 List 都提供了 iterator() 方法的实现。

Iterator is also an interface which defines the iteration process it provides next(), hasNext() and remove() method which can be used to control the iteration, access the element and remove it. Iterator 也是一个接口,它定义了迭代过程,它提供了 next()、hasNext() 和 remove() 方法,可用于控制迭代、访问元素并删除它。

You use Iterator<String> for the same reason you use String in您使用Iterator<String>的原因与在其中使用String原因相同

String stringx = x.toString()

the method (correct name for "module") declares that it returns you an object of that type.方法(“模块”的正确名称)声明它返回一个该类型的对象。 Methods can return any unrelated type and you'll have to adjust and use the variable type they give you.方法可以返回任何不相关的类型,您必须调整和使用它们给您的变量类型。

Iterator is conceptually related to Iterable but they don't inherit from each other or so. Iterator在概念上与Iterable相关,但它们并不相互继承。 And it's actually just convention that the Iterator grants you access to the same data as the Iterable that produced it.实际上, Iterator允许您访问与生成它的Iterable相同的数据,这实际上只是约定。

But the design / convention for Iterator is that you can iterate once in the lifetime of an iterator over the collection of items (repeating hasNext() ? Ok give next() ! over and over until there is no next.).但是Iterator的设计/约定是,您可以在迭代器的生命周期中对项目集合进行一次迭代(重复hasNext() ?好的给next() !一遍又一遍,直到没有下一个。)。 If you want to iterate again you'll have to get a fresh Iterator .如果你想再次迭代,你必须得到一个新的Iterator And that's the responsibility of Iterable : It can produce a fresh Iterator every time you ask it.这就是Iterable的责任:它可以在您每次询问时生成一个新的Iterator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM