Home

ArrayBuffer

ArrayBuffer

An ArrayBuffer object stores an array and its current length. It supports efficient append operations. You can add elements using +=. When you are done, the toArray method returns the contents as a normal array.

scala> val A = scala.collection.mutable.ArrayBuffer.empty[Int]
A: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> A += 1
res9: A.type = ArrayBuffer(1)
scala> for (i <- 2 to 100)
     |   A += i
scala> A
res11: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2,
3, 4, ... 100)
scala> A.toArray
res12: Array[Int] = Array(1, 2, 3, 4, 5, 6, ..., 99, 100)

ArrayBuffer provides all the common methods of sequences.