Scala迭代器

Scala Iterator(迭代器)不是一个集合,它是一种用于访问集合的方法。迭代器 it 的两个基本操作是 next 和 hasNext。

调用 it.next() 会返回迭代器的下一个元素,并且更新迭代器的状态。调用 it.hasNext() 用于检测集合中是否还有元素。

让迭代器 it 逐个返回所有元素最简单的方法是使用 while 循环:

object HaiCoder { def main(args: Array[String]) : Unit = { println("嗨客网(www.haicoder.net)!\n") val it = Iterator("Baidu", "Google", "HaiCoder", "Taobao") while (it.hasNext){ println(it.next()) } } }

执行以上代码,输出结果为:

01_Scala迭代器iterator.png

我们创建了迭代器,并遍历了所有元素。

Scala迭代器总结

Scala Iterator(迭代器)不是一个集合,它是一种用于访问集合的方法。迭代器 it 的两个基本操作是 next 和 hasNext。