第十章 Scala 容器基礎(十一):使用zip或者zipWithIndex來建立一個循環計數器

Problem

    你要遍歷一個有序集合,同時你又想訪問一個循環計數器,但最重要的是你真的不須要手動建立這個計數器。
app

Solution

    使用zipWithIndex活着zip方法來自動地建立一個計數器,假設你有一個有序集合days,那麼你能夠使用zipWithIndex和counter來打印帶有計數器的集合元素:ui

scala> val days = Array("Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday")
days: Array[String] = Array(Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)

scala> days.zipWithIndex.foreach{case(day,count) => println(s"$count is $day")}
0 is Sunday
1 is Monday
2 is Tuesday
3 is Wednesday
4 is Thursday
5 is Friday
6 is Saturday

    一樣,你能夠使用for循環來打印計數器和集合元素spa

scala> for((day,count) <- days.zipWithIndex) {
     |   println(s"$count is $day")
     | }
0 is Sunday
1 is Monday
2 is Tuesday
3 is Wednesday
4 is Thursday
5 is Friday
6 is Saturday

    zipWithIndex的計數器都是從0開始,若是你想指定開始的值,那麼你能夠使用zip Stream:scala

scala> for((day,count) <- days.zip(Stream from 1)) {
     |   println(s"$count is $day")
     | }
1 is Sunday
2 is Monday
3 is Tuesday
4 is Wednesday
5 is Thursday
6 is Friday
7 is Saturday

Discussion

    當有序集合調用zipWithIndex的時候,它會返回一個有序的二元組集合:code

scala> val list = List("a", "b", "c")
list: List[String] = List(a, b, c)

scala> list.zipWithIndex
res3: List[(String, Int)] = List((a,0), (b,1), (c,2))

    由於zipWithIndex是在一個已經存的有序集合上基礎上創建一個新的有序集合,你能夠在調用zipWithIndex以前調用view:ip

scala> val zwv = list.view.zipWithIndex
zwv: scala.collection.SeqView[(String, Int),Seq[_]] = SeqViewZ(...)

    就像上面這個例子裏面看到的,它建立了一個lazy view在原有的List基礎上,因此這個元組集合並不被會被建立,直到它被調用的那一刻。正因有這種特性,咱們推薦在調用zipWithIndex以前先調用view方法。
element

    zip和zipWithIndex方法都返回一個有序二元祖集合。所以,你的foreach方法也能夠寫成下面這樣,雖然這比起解決方案中的方法,可讀性略差。it

scala> days.zipWithIndex.foreach(d => println(s"${d._2} is ${d._1}"))
0 is Sunday
1 is Monday
2 is Tuesday
3 is Wednesday
4 is Thursday
5 is Friday
6 is Saturday

    在以前的例子中咱們曾經見過,能夠經過一個for循環加range來建立這個計數器:io

scala> val fruits = Array("apple", "banana", "orange")
fruits: Array[String] = Array(apple, banana, orange)

scala> for (i <- 0 until fruits.size) println(s"element $i is ${fruits(i)}")
element 0 is apple
element 1 is banana
element 2 is orange
相關文章
相關標籤/搜索