1. Overview
In this tutorial, we’ll dig into the Scala ArrayBuffer class, which is one of the many available collections in the standard library.
2. Scala Collections
Scala provides a wide range of different collections like List, Set, Map, and many others not so commonly used. The Scala ArrayBuffer is one of those.
There are two main groups of collections in Scala: immutable and mutable collections. While the Scala community is biased towards the usage of immutable data structures, the standard lib also provides many mutable collections. One such example is the ArrayBuffer class which is very similar to normal Java Lists.
3. ArrayBuffer
An ArrayBuffer is a specific implementation of a mutable list. This means that we can keep adding more elements to the list after the creation moment. If we exceed the current max size of the list, then it will allocate another bigger in-memory array and copy all the elements there. For immutable lists, a new list is always created if we try to append another element.
Another important detail is that the ArrayBuffer implementation in specific is backed up by an indexed array, meaning that we have constant access to any element of the collection.
3.1. Creating a New ArrayBuffer
Creating a new instance is very straightforward:
scala> import scala.collection.mutable.ArrayBuffer
scala> val nums = ArrayBuffer(1, 2, 3)
val nums: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
And we have our new instance.
3.2. Adding a New Element
To add an element, there are a few different solutions:
scala> import scala.collection.mutable.ArrayBuffer
scala> val l = ArrayBuffer(1, 2, 3)
val l: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
scala> l += 4
val res0: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4)
scala> l.append(5)
val res1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
scala> l.prepend(0)
val res2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 1, 2, 3, 4, 5)
scala> l.insert(0, -1)
scala> l
val res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-1, 0, 1, 2, 3, 4, 5)
ArrayBuffer allows us to insert a new element both at the beginning and end of the list. Furthermore, since it’s an indexed list, it’s also easy to insert an element in any position of the list.
3.3. Removing an Element
To remove elements, there are also a few different methods available:
scala> import scala.collection.mutable.ArrayBuffer
scala> val l = ArrayBuffer(1, 2, 3, 4, 5, 6, 7)
val l: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6, 7)
scala> l -= 3
val res1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 4, 5, 6, 7)
scala> l.remove(3,2) // starting from index 3, remove 2 elements
scala> l
val res2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 4, 7)
Similar to the insertion, we can also remove elements from any position of the list.
3.4. Appending Another List
If we need to concatenate another List or any other collection, we can use the ArrayBuffer#appendAll method:
scala> val l = ArrayBuffer(1, 2, 3)
val l: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
scala> l.appendAll(List(4,5,6))
val res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6)
4. Conclusion
In this article, we saw what is the ArrayBuffer class and some of the more common operations.