什么时候实现Iterator但不是Iterable有用吗? [英] When would it be useful to implement Iterator but not Iterable?
问题描述
什么时候实现迭代器而不实现iterable会有用吗?
When would it be useful to implement iterator without implementing iterable?
或者实现iterator只是实现iterable的副产品?
Or is implementing iterator simply a by product of implementing iterable?
推荐答案
这两个是相关但不相同的。
These two are related but not the same.
A List
是 Iterable
- 你可以得到它的 Iterator
。 不 Iterator
。
A List
is Iterable
- you can get its Iterator
. It is not an Iterator
.
Iterator
是一个单独使用的类,可以使用 hasNext
和< Collection
对象code> next 方法。
An Iterator
is a single use class that can iterate along a Collection
of objects using the hasNext
and next
methods.
Iterable
是 Collection
返回 Iterator
实例的类,然后是 iterator()
方法被叫。
An Iterable
is a Collection
class that returns an Iterator
instance when then the iterator()
method is called.
我会说我看到没有的情况,其中 Iterable
应该实现Iterator
。由于 Iterator
是单用的,我看不到 Iterator
应该实现Iterable的情况
。
I would go as far as to say that I see no case where an Iterable
should implements Iterator
. And as a Iterator
is single use I cannot see a case where Iterator
should implements Iterable
.
这篇关于什么时候实现Iterator但不是Iterable有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!