scala集合-List
参考:https://www.runoob.com/scala/scala-lists.html
https://www.jianshu.com/p/24c0ed2e3ae8
1. 默认scala的List是可不变的,值一旦被定义了就不能改变了,所以列表改变后都需要重新赋值。
2. ListBuffer(可变长集合)
2020-04-22 |
1.导包 import scala.collection.mutable.ListBuffer 2. 声明 scala> val lb = ListBuffer(1,4) lb: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1,4) 3.常用操作 增加元素 scala> lb += 5 res17: lb.type = ListBuffer(1, 4, 5) scala> lb ++= List(10, 20) res18: lb.type = ListBuffer(1, 4, 5, 10, 20) scala> lb ++= Array('b', 'c') res19: lb.type = ListBuffer(1, 4, 5, 10, 20, 98, 99) 按照下标插入几个元素 scala> lb.insert(0, 2 ,2, 3) scala> lb res21: scala.collection.mutable.ListBuffer[Int] = ListBuffer(2, 2, 3, 1, 4, 5, 10, 20, 98, 99) 按照值删除元素 scala> lb -= 99 res23: lb.type = ListBuffer(2, 2, 3, 1, 4, 5, 10, 20, 98) scala> lb --= Array(2, 3, 1, 4) res24: lb.type = ListBuffer(2,5, 10, 20, 98) 按照下标删除几个元素 scala> lb.remove(0, 2) scala> lb res26: scala.collection.mutable.ListBuffer[Int] = ListBuffer(10, 20, 98) 根据下标更新元素值 scala> lb(0) = 33 scala> lb res28: scala.collection.mutable.ListBuffer[Int] = ListBuffer(33, 20, 98) 与Array转换 scala> lb.toArray res27: Array[Int] = Array(10, 20, 98)