1. 程式人生 > >scala 陣列ArrayBuffer

scala 陣列ArrayBuffer

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val arrBuffer=ArrayBuffer[Int]()
arrBuffer: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

scala> arrBuffer+=10
res6: arrBuffer.type = ArrayBuffer(10)

scala> arrBuffer
res7: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10)

scala> arrBuffer+=(11,1,3,4,5)
res8: arrBuffer.type = ArrayBuffer(10, 11, 1, 3, 4, 5)

scala> arrBuffer++=Array(1,2,3,4)
res9: arrBuffer.type = ArrayBuffer(10, 11, 1, 3, 4, 5, 1, 2, 3, 4)

scala> arrBuffer.trim
trimEnd     trimStart

scala> arrBuffer.trimEnd(3)

scala> arrBuffer
res11: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 11, 1, 3, 4, 5, 1)

scala> arrBuffer.insert(5,100)

scala> arrBuffer
res13: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 11, 1, 3, 4, 100, 5, 1)

scala> arrBuffer.insert(7,9,2,444,5)

scala> arrBuffer
res15: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 11, 1, 3, 4, 100, 5, 9, 2, 444, 5, 1)

scala> arrBuffer.remove(10)
res16: Int = 5

scala> arrBuffer
res17: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 11, 1, 3, 4, 100, 5, 9, 2, 444, 1)

scala> arrBuffer.remove(6)
res18: Int = 5

scala> arrBuffer
res19: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 11, 1, 3, 4, 100, 9, 2, 444, 1)


scala> arrBuffer.remove(6,3)

scala> arrBuffer
res25: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 11, 1, 3, 4, 100, 1)

scala> arrBuffer.toArray
res26: Array[Int] = Array(10, 11, 1, 3, 4, 100, 1)

scala> val arr2=arrBuffer.toArray
arr2: Array[Int] = Array(10, 11, 1, 3, 4, 100, 1)

scala> arr2.toBuffer
res27: scala.collection.mutable.Buffer[Int] = ArrayBuffer(10, 11, 1, 3, 4, 100, 1)