# Scala Iterator(迭代器)
Scala Iterator(迭代器)不是一個集合,它是一種用于訪問集合的方法。
迭代器 it 的兩個基本操作是 **next** 和 **hasNext** 。
調用 **it.next()** 會返回迭代器的下一個元素,并且更新迭代器的狀態。
調用 **it.hasNext()** 用于檢測集合中是否還有元素。
讓迭代器 it 逐個返回所有元素最簡單的方法是使用 while 循環:
```
object Test {
def main(args: Array[String]) {
val it = Iterator("Baidu", "Google", "Runoob", "Taobao")
while (it.hasNext){
println(it.next())
}
}
}
```
執行以上代碼,輸出結果為:
```
$ scalac Test.scala
$ scala Test
Baidu
Google
Runoob
Taobao
```
## 查找最大與最小元素
你可以使用 **it.min** 和 **it.max** 方法從迭代器中查找最大與最小元素,實例如下:
```
object Test {
def main(args: Array[String]) {
val ita = Iterator(20,40,2,50,69, 90)
val itb = Iterator(20,40,2,50,69, 90)
println("最大元素是:" + ita.max )
println("最小元素是:" + itb.min )
}
}
```
執行以上代碼,輸出結果為:
```
$ scalac Test.scala
$ scala Test
最大元素是:90
最小元素是:2
```
## 獲取迭代器的長度
你可以使用 **it.size** | 或 **it.length** | 方法來查看迭代器中的元素個數。實例如下:
```
object Test {
def main(args: Array[String]) {
val ita = Iterator(20,40,2,50,69, 90)
val itb = Iterator(20,40,2,50,69, 90)
println("ita.size 的值: " + ita.size )
println("itb.length 的值: " + itb.length )
}
}
```
執行以上代碼,輸出結果為:
```
$ scalac Test.scala
$ scala Test
ita.size 的值: 6
itb.length 的值: 6
```
## Scala Iterator 常用方法
下表列出了 Scala Iterator 常用的方法:
| 方法 | 描述 |
| --- | --- |
| **def hasNext: Boolean** | 如果還有可返回的元素,返回true。 |
| **def next(): A** | 返回迭代器的下一個元素,并且更新迭代器的狀態 |
| **def ++(that: => Iterator\[A\]): Iterator\[A\]** | 合并兩個迭代器 |
| **def ++\[B >: A\](that :=> GenTraversableOnce\[B\]) |: Iterator\[B\]** | 合并兩個迭代器 |
| **def addString(b: StringBuilder): StringBuilder** | 添加一個字符串到 StringBuilder b |
| **def addString(b: StringBuilder, sep: String): StringBuilder** | 添加一個字符串到 StringBuilder b,并指定分隔符 |
| **def buffered: BufferedIterator\[A\]** | 迭代器都轉換成 BufferedIterator |
| **def contains(elem: Any): Boolean** | 檢測迭代器中是否包含指定元素 |
| **def copyToArray(xs: Array\[A\], start: Int, len: Int): Unit** | 將迭代器中選定的值傳給數組 |
| **def count(p: (A) => Boolean): Int** | 返回迭代器元素中滿足條件p的元素總數。 |
| **def drop(n: Int): Iterator\[A\]** | 返回丟棄前n個元素新集合 |
| **def dropWhile(p: (A) => Boolean): Iterator\[A\]** | 從左向右丟棄元素,直到條件p不成立 |
| **def duplicate: (Iterator\[A\], Iterator\[A\])** | 生成兩個能分別返回迭代器所有元素的迭代器。 |
| **def exists(p: (A) => Boolean): Boolean** | 返回一個布爾值,指明迭代器元素中是否存在滿足p的元素。 |
| **def filter(p: (A) => Boolean): Iterator\[A\]** | 返回一個新迭代器 ,指向迭代器元素中所有滿足條件p的元素。 |
| **def filterNot(p: (A) => Boolean): Iterator\[A\]** | 返回一個迭代器,指向迭代器元素中不滿足條件p的元素。 |
| **def find(p: (A) => Boolean): Option\[A\]** | 返回第一個滿足p的元素或None。注意:如果找到滿足條件的元素,迭代器會被置于該元素之后;如果沒有找到,會被置于終點。 |
| **def flatMap\[B\](f: (A) | => GenTraversableOnce\[B\]): Iterator\[B\]** | 針對迭代器的序列中的每個元素應用函數f,并返回指向結果序列的迭代器。 |
| **def forall(p: (A) => Boolean): Boolean** | 返回一個布爾值,指明 it 所指元素是否都滿足p。 |
| **def foreach(f: (A) => Unit): Unit** | 在迭代器返回的每個元素上執行指定的程序 f |
| **def hasDefiniteSize: Boolean** | 如果迭代器的元素個數有限則返回true(缺省等同于isEmpty) |
| **def indexOf(elem: B): Int** | 返回迭代器的元素中index等于x的第一個元素。注意:迭代器會越過這個元素。 |
| **def indexWhere(p: (A) => Boolean): Int** | 返回迭代器的元素中下標滿足條件p的元素。注意:迭代器會越過這個元素。 |
| **def isEmpty: Boolean** | 檢查it是否為空, 為空返回 true,否則返回false(與hasNext相反)。 |
| **def isTraversableAgain: Boolean** | Tests whether this Iterator can be repeatedly traversed. |
| **def length: Int** | 返回迭代器元素的數量。 |
| **def map\[B\](f: (A) | => B): Iterator\[B\]** | 將 it 中的每個元素傳入函數 f 后的結果生成新的迭代器。 |
| **def max: A** | 返回迭代器迭代器元素中最大的元素。 |
| **def min: A** | 返回迭代器迭代器元素中最小的元素。 |
| **def mkString: String** | 將迭代器所有元素轉換成字符串。 |
| **def mkString(sep: String): String** | 將迭代器所有元素轉換成字符串,并指定分隔符。 |
| **def nonEmpty: Boolean** | 檢查容器中是否包含元素(相當于 hasNext)。 |
| **def padTo(len: Int, elem: A): Iterator\[A\]** | 首先返回迭代器所有元素,追加拷貝 elem 直到長度達到 len。 |
| **def patch(from: Int, patchElems: Iterator\[B\], replaced: Int): Iterator\[B\]** | 返回一個新迭代器,其中自第 from 個元素開始的 replaced 個元素被迭代器所指元素替換。 |
| **def product: A** | 返回迭代器所指數值型元素的積。 |
| **def sameElements(that: Iterator[_]): Boolean** | 判斷迭代器和指定的迭代器參數是否依次返回相同元素 |
| **def seq: Iterator\[A\]** | 返回集合的系列視圖 |
| **def size: Int** | 返回迭代器的元素數量 |
| **def slice(from: Int, until: Int): Iterator\[A\]** | 返回一個新的迭代器,指向迭代器所指向的序列中從開始于第 from 個元素、結束于第 until 個元素的片段。 |
| **def sum: A** | 返回迭代器所指數值型元素的和 |
| **def take(n: Int): Iterator\[A\]** | 返回前 n 個元素的新迭代器。 |
| **def toArray: Array\[A\]** | 將迭代器指向的所有元素歸入數組并返回。 |
| **def toBuffer: Buffer\[B\]** | 將迭代器指向的所有元素拷貝至緩沖區 Buffer。 |
| **def toIterable: Iterable\[A\]** | Returns an Iterable containing all elements of this traversable or iterator. This will not terminate for infinite iterators. |
| **def toIterator: Iterator\[A\]** | 把迭代器的所有元素歸入一個Iterator容器并返回。 |
| **def toList: List\[A\]** | 把迭代器的所有元素歸入列表并返回 |
| **def toMap[T, U]: Map[T, U]** | 將迭代器的所有鍵值對歸入一個Map并返回。 |
| **def toSeq: Seq\[A\]** | 將代器的所有元素歸入一個Seq容器并返回。 |
| **def toString(): String** | 將迭代器轉換為字符串 |
| **def zip\[B\](that: Iterator\[B\]) |: Iterator[(A, B)** | 返回一個新迭代器,指向分別由迭代器和指定的迭代器 that 元素一一對應而成的二元組序列 |
更多方法可以參考 [API文檔](http://www.scala-lang.org/api/current/index.html#scala.collection.Iterator)
- Scala 教程
- Scala 簡介
- Scala 安裝
- Scala 基礎語法
- Scala 數據類型
- Scala 變量
- Scala 訪問修飾符
- Scala 運算符
- Scala IF...ELSE 語句
- Scala 循環
- Scala while 循環
- Scala do...while 循環
- Scala do...while 循環
- Scala break 語句
- Scala 函數
- Scala 函數傳名調用(call-by-name)
- Scala 指定函數參數名
- Scala 函數 - 可變參數
- Scala 遞歸函數
- Scala 高階函數
- Scala 函數嵌套
- Scala 匿名函數
- Scala 偏應用函數
- Scala 函數柯里化(Currying)
- Scala 閉包
- Scala 字符串
- Scala 數組
- Scala Collection
- Scala List(列表)
- Scala Set(集合)
- Scala Map(映射)
- Scala 元組
- Scala Option(選項)
- Scala Iterator(迭代器)
- Scala 類和對象
- Scala Trait(特征)
- Scala 模式匹配
- Scala 正則表達式
- Scala 異常處理
- Scala 提取器(Extractor)
- Scala 文件 I/O