Home

ListBuffer

ListBuffer

A List can be built up efficiently only from back to front. When you want to build a list from front to back, the ListBuffer object is convenient. It support efficient prepend and append operations. Once you are done creating your list, call the toList method. This takes only constant time to convert the ListBuffer into a List.

ListBuffer also provides all the common methods of sequences.

For example:

scala> val S = scala.collection.mutable.ListBuffer.empty[Int]
S: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
scala> S += 7
res1: S.type = ListBuffer(7)
scala> S += 9
res2: S.type = ListBuffer(7, 9)
scala> 1 +=: S
res3: S.type = ListBuffer(1, 7, 9)
scala> S.toList
res4: List[Int] = List(1, 7, 9)