Scala学习笔记-06-数据结构-ListBuffer 和 ArrayBuffer

时间:2020-02-13 16:51:23   收藏:0   阅读:309

对于ListBuffer, += 等效于insert, -= 等效于remove

scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer
// 构造ListBuffer
scala> val mutList1 = ListBuffer(10,20,30)
mutList1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(10, 20, 30)
// 在mutList1基础上,增加50在最后
scala> mutList1 += 50
res58: mutList1.type = ListBuffer(10, 20, 30, 50)

scala> mutList1
res59: scala.collection.mutable.ListBuffer[Int] = ListBuffer(10, 20, 30, 50)
// 在mutList1基础上增加60元素,并购造一个新的List,原mutList1不变
scala> mutList1 :+ 60
res60: scala.collection.mutable.ListBuffer[Int] = ListBuffer(10, 20, 30, 50, 60)

scala> mutList1
res61: scala.collection.mutable.ListBuffer[Int] = ListBuffer(10, 20, 30, 50)

scala> res60
res62: scala.collection.mutable.ListBuffer[Int] = ListBuffer(10, 20, 30, 50, 60)
// mutList1基础上,在第二个索引位置增加60元素
scala> mutList1.insert(2,100)

scala> mutList1
res64: scala.collection.mutable.ListBuffer[Int] = ListBuffer(10, 20, 100, 30, 50)
//在mutList1基础上,删除第一个元素100
scala> mutList1 -= 100
res65: mutList1.type = ListBuffer(10, 20, 30, 50)

scala> mutList1
res66: scala.collection.mutable.ListBuffer[Int] = ListBuffer(10, 20, 30, 50)
// 在mutList1基础上,删除索引为3的元素
scala> mutList1.remove(3)
res68: Int = 50

scala> mutList1
res69: scala.collection.mutable.ListBuffer[Int] = ListBuffer(10, 20, 30)

 

 

----

原文:https://www.cnblogs.com/wooluwalker/p/12303919.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!