遍历集合的方法
1.用foreach循环遍历一个集合
foreach接收一个函数作为参数。定义的函数应该接收一个元素作为输入参数,然后不要返回任何的东西。输入的参数的类型应该匹配集合中的类型。随着foreach的执行,它每次都会把一个元素传给你的函数,直到集合中最后一个元素。
foreach常用的就是输出信息:
scala> val x = Vector(1,2,3)
x: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3) scala> x.foreach((i:Int) => println(i))
1
2
3
对于表达式,还有其他写法:
x.foreach(i => println(i)) x.foreach(println(_)) x.foreach(println)
对于最后一种表达式,情况是一个只有一条语句组成并且接受一个参数的函数可以简写成这种形式。
2.用for循环遍历一个集合
可以用for循环遍历任意一种Traversable的类型。
scala> val fruits = Traversable("apple","orange","banana")
fruits: Traversable[String] = List(apple, orange, banana) scala> for(i <- fruits) println(i)
apple
orange
banana
如果算法很长,可以在for循环的代码块里执行。
下面展示了for循环里使用计数器的几种方式:
scala> val fruits = Array("apple","banana","orange")
fruits: Array[String] = Array(apple, banana, orange) scala> for(i <- 0 until fruits.size) println(s"$i is ${fruits(i)}")
0 is apple
1 is banana
2 is orange
scala> val fruits = Array("apple","banana","orange")
fruits: Array[String] = Array(apple, banana, orange) scala> for((elem,count) <- fruits.zipWithIndex){
| println(s"$count is $elem")
| }
0 is apple
1 is banana
2 is orange
scala> val fruits = Array("apple","banana","orange")
fruits: Array[String] = Array(apple, banana, orange) scala> for((elem,count) <- fruits.zip(Stream from 1)){
| println(s"$count is $elem")
| }
1 is apple
2 is banana
3 is orange
3.用zipWithIndex创建循环计数器
(1)以foreach的形式遍历集合
scala> val days = Array("Sunday","Monday","Tusday","Wednsday","Thursday","Friday","Saturday")
days: Array[String] = Array(Sunday, Monday, Tusday, Wednsday, Thursday, Friday,Saturday) scala> days.zipWithIndex.foreach{
| case(day,count) => println(s"${count+1} is $day")
| }
1 is Sunday
2 is Monday
3 is Tusday
4 is Wednsday
5 is Thursday
6 is Friday
7 is Saturday
另外的一种表示方法:
scala> val days = Array("Sunday","Monday","Tusday","Wednsday","Thursday","Friday","Saturday")
days: Array[String] = Array(Sunday, Monday, Tusday, Wednsday, Thursday, Friday,Saturday) scala> days.zipWithIndex.foreach{ d=>
| println(s"${d._2+1} is ${d._1}")
| }
1 is Sunday
2 is Monday
3 is Tusday
4 is Wednsday
5 is Thursday
6 is Friday
7 is Saturday
(2) 以for的形式循环遍历
scala> val days = Array("Sunday","Monday","Tusday","Wednsday","Thursday","Friday","Saturday")
days: Array[String] = Array(Sunday, Monday, Tusday, Wednsday, Thursday, Friday,Saturday) scala> for ((day,count) <- days.zipWithIndex){
| println(s"${count+1} is $day")
| }
1 is Sunday
2 is Monday
3 is Tusday
4 is Wednsday
5 is Thursday
6 is Friday
7 is Saturday
4.用zip创建循环计数器
在zip中使用Stream是一种生存计数器的方法。
scala> val days = Array("Sunday","Monday","Tusday","Wednsday","Thursday","Friday","Saturday")
days: Array[String] = Array(Sunday, Monday, Tusday, Wednsday, Thursday, Friday,Saturday) scala> for ((day,count) <- days.zip(Stream from 1)){
| println(s"$count is $day")
| }
1 is Sunday
2 is Monday
3 is Tusday
4 is Wednsday
5 is Thursday
6 is Friday
7 is Saturday
5.range循环计数器
如果只是需要重复做某事多次,可以用range。
scala> val fruits = Array("apple","banana","orange")
fruits: Array[String] = Array(apple, banana, orange) scala> for(i <- 0 until fruits.size) {
| println(s"$i is ${fruits(i)}")}
0 is apple
1 is banana
2 is orange
6.用reduce方法遍历集合
使用reduceLeft和reduceRight方法来遍历序列中的元素,把相邻的元素传给你的函数成成一个新的结果,之后和序列的下一个元素比较在生成新的结果。
reduceLeft方法是从左到右遍历一个序列,在算法中首先会对前两个元素进行比较,然后返回一个结果。该结果会与第三个元素进行比较,比较之后在产生一个新结果,接着在于第四个元素比较,以此类推。
scala> val a = Array(12,3,4,5,67)
a: Array[Int] = Array(12, 3, 4, 5, 67) scala> a.reduceLeft(_ + _)
res4: Int = 91 scala> a.reduceLeft(_ min _)
res5: Int = 3 scala> a.reduceLeft(_ max _)
res6: Int = 67
这里面的两个下划线,它们代表传给函数的两个参数。
reduceRight与reduceLeft同理的。
7.用fold方法遍历集合
foldLeft方法如同reduceLeft,它会设置一个种子值用于第一个元素。foldLeft接收两个参数列表。第一个列表有一个字段,种子值。第二个列表是要运行的代码块。
scala> val a = Array(12,3,4,5,67)
a: Array[Int] = Array(12, 3, 4, 5, 67) scala> a.reduceLeft(_ + _)
res8: Int = 91 scala> a.foldLeft(20)(_ + _)
res9: Int = 111
我们来看一下foldLeft和foldRight的执行过程:
下面的/:是foldLeft的简化版。
scala> ((1 to 4).foldLeft(5))((i,sum) => i-sum)
res25: Int = -5 scala> (1 to 4).foldLeft(5)(_-_)
res8: Int = -5 scala> (5/:(1 to 4))(_-_)
res9: Int = -5
foldLeft的执行过程:5-1-2-3-4=-5
下面的:\是foldRight的简化版。
scala> ((1 to 4).foldRight(5))((i,sum) => i-sum)
res22: Int = 3 scala> (1 to 4).foldRight(5)(_-_)
res14: Int = 3 scala> ((1 to 4):\5)(_-_)
res15: Int = 3
foldRight的执行过程:1-(2-(3-(4-(5-0))))=3