Array和List中通用且重要的方法
1.max
2.min
3.sum
4.take
5.takeRight
6.drop
7.dropRight
8.head
9.last
10.union 并集
11.intersect 交集
12.diff 差集
13.distinct
14.mkString
15.reverse
16.filter
17.map
18.sortBy
19.reduce
20.flatMap
21.groupBy
重点掌握:① filter ② map ③sortBy ④reduce ⑤flatMap ⑥groupBy
object Demo06 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
//创建一个定长List并赋值
val l1=List(1,2,3,4) //> l1 : List[Int] = List(1, 2, 3, 4)
//创建一个变长List并赋值
val l2=scala.collection.mutable.ListBuffer(1,2,3,4)
//> l2 : scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4)
//通过下标操作List
l1.apply(0) //> res0: Int = 1
l1(0) //> res1: Int = 1
//Array和List之间可以相互转换
val a1=l1.toArray //> a1 : Array[Int] = Array(1, 2, 3, 4)
val l3=a1.toList //> l3 : List[Int] = List(1, 2, 3, 4)
//map方法。映射方法
//根据传入的匿名函数,将元素从一个形式映射为另外一个形式
//最后将结果返回到新的集合中
val r1=l1.map {
num => num*2 } //> r1 : List[Int] = List(2, 4, 6, 8)
val r2=l1.map {