Iterable和iterator

来源:互联网 发布:软件外包公司资质 编辑:程序博客网 时间:2024/05/21 14:50

(1) Iterable:大部分collection直接实现了改接口(trait) 


A base trait for iterable collections.

This is a base trait for all Scala collections that define an iterator method to step through one-by-one the collection's elements. Implementations of this trait need to provide a concrete method with signature: 

def iterator: Iterator[A]


(2) iterator:易变的,An iterator is mutable, 含有hastNext 和next( )两个方法来遍历元素


Iterators are data structures that allow to iterate over a sequence of elements. They have a hasNext method for checking if there is a next element available, and a next method which returns the next element and advances the iterator.

An iterator is mutable: most operations on it change its state. While it is often used to iterate through the elements of a collection, it can also be used without being backed by any collection (see constructors on the companion object).

It is of particular importance to note that, unless stated otherwise, one should never use an iterator after calling a method on it. The two most important exceptions are also the sole abstract methods: next and hasNext.



原创粉丝点击