問題:
你要遍歷一個有序集合,同時你又想訪問一個循環計數器,但最重要的是你真的不須要手動建立這個計數器。
解決方案:
使用zipWithIndex或者zip方法來自動地建立一個計數器,假設你有一個有序集合days,那麼你能夠使用zipWithIndex和counter來打印帶有計數器的集合元素:app
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循環來打印計數器和集合元素ui
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:spa
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
當有序集合調用zipWithIndex的時候,它會返回一個有序的二元組集合:scala
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:code
就像上面這個例子裏面看到的,它在原有的List基礎上建立了一個lazy view,因此這個元組集合並不被會被建立,直到它被調用的那一刻。正因有這種特性,咱們推薦在調用zipWithIndex以前先調用view方法。blog
zip和zipWithIndex方法都返回一個有序二元組集合。所以,你的foreach方法也能夠寫成下面這樣,雖然這比起解決方案中的方法,可讀性略差。ip
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來建立這個計數器:element
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