What is the difference between iterator and iterable and how to use them?(迭代器和可迭代之间有什么区别以及如何使用它们?)
问题描述
我是 Java 新手,我对迭代器和可迭代感到非常困惑.谁能给我解释一下并举一些例子?
I am new in Java and I'm really confused with iterator and iterable. Can anyone explain to me and give some examples?
推荐答案
Iterable
是一系列可以迭代的元素的简单表示.它没有任何迭代状态,例如当前元素".相反,它有一种方法可以生成 Iterator
.
An Iterable
is a simple representation of a series of elements that can be iterated over. It does not have any iteration state such as a "current element". Instead, it has one method that produces an Iterator
.
Iterator
是具有迭代状态的对象.它允许您使用 hasNext()
检查它是否有更多元素,并使用 next()
移动到下一个元素(如果有).
An Iterator
is the object with iteration state. It lets you check if it has more elements using hasNext()
and move to the next element (if any) using next()
.
通常,Iterable
应该能够产生任意数量的有效 Iterator
.
Typically, an Iterable
should be able to produce any number of valid Iterator
s.
这篇关于迭代器和可迭代之间有什么区别以及如何使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:迭代器和可迭代之间有什么区别以及如何使用它


基础教程推荐
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01