問題描述
我是 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.
這篇關于迭代器和可迭代之間有什么區別以及如何使用它們?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!