为集合添加序号.
val itemsCosts = Seq(("Pencil", 0.52), ("Paper", 1.35), ("Notebook", 2.43))
val itemsCostsIndices = itemsCosts.zipWithIndex
for (itemCostsIndex <- itemsCostsIndices) {
itemCostsIndex match {
case ((item, cost), index) => println(s"$index $item costs $cost each")
}
}
> scala ZipWithIn.scala
0 Pencil costs 0.52 each
1 Paper costs 1.35 each
2 Notebook costs 2.43 each