这个主要是能给目前已有的Seq加上一个索引序号,它的返回值还是一个Seq
A new sequence containing pairs consisting of all elements of
this sequence paired with their index. Indices start at 0.
例子
List("a", "b", "c").zipWithIndex = List(("a", 0), ("b", 1), ("c", 2))
val itemsCosts = Seq(("Pencil", 0.52), ("Paper", 1.35), ("Notebook", 2.43))
val itemsCostsIndices = itemsCosts.zipWithIndex
for(itemsCostsIndex<-itemsCostsIndices) {
itemsCostsIndex match {
case ((item, price), index) => println(s"$index:the $item is $price")
}
}
输出
0:the Pencil is 0.52
1:the Paper is 1.35
2:the Notebook is 2.43