迭代器和迭代之间有什么区别以及如何使用它们? [英] What is the difference between iterator and iterable and how to use them?

查看:121
本文介绍了迭代器和迭代之间有什么区别以及如何使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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.

一个迭代器是具有迭代状态的对象。它允许您使用 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 应该能够生成任意数量的有效迭代器 s。

Typically, an Iterable should be able to produce any number of valid Iterators.

这篇关于迭代器和迭代之间有什么区别以及如何使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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