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

对于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)

 

 

----

你可能感兴趣的:(Scala学习笔记-06-数据结构-ListBuffer 和 ArrayBuffer)