Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
ArrayBuffer
is a mutable, indexed sequence that can be resized (i.e., you can add or remove elements). In Scala, ArrayBuffer
resides in the scala.collection.mutable
package. Because it's mutable, you can change, add, or remove elements from it after it's been created.
Here's a guide to working with ArrayBuffer
in Scala:
import scala.collection.mutable.ArrayBuffer val buffer = ArrayBuffer[Int]() val bufferWithInitialElements = ArrayBuffer(1, 2, 3, 4)
You can add elements to an ArrayBuffer
using +=
for one element and ++=
for multiple elements.
buffer += 5 // ArrayBuffer(5) buffer += (6,7) // ArrayBuffer(5, 6, 7) buffer ++= Seq(8,9,10) // ArrayBuffer(5, 6, 7, 8, 9, 10)
To prepend elements, use +=:
1 +=: buffer // ArrayBuffer(1, 5, 6, 7, 8, 9, 10)
You can remove elements using -=
and --=
.
buffer -= 10 // ArrayBuffer(1, 5, 6, 7, 8, 9) buffer --= Seq(8,9) // ArrayBuffer(1, 5, 6, 7)
Since ArrayBuffer
is indexed, you can access and modify its elements using their index.
val thirdElement = buffer(2) // 6 buffer(2) = 60 // ArrayBuffer(1, 5, 60, 7)
length: Returns the number of elements.
val size = buffer.length // 4
trimEnd: Reduces the size of the buffer.
buffer.trimEnd(1) // ArrayBuffer(1, 5, 60)
insert: Inserts new elements at a specific index.
buffer.insert(1, 15) // ArrayBuffer(1, 15, 5, 60)
remove: Removes elements at a specific index.
buffer.remove(1) // ArrayBuffer(1, 5, 60)
clear: Removes all elements.
buffer.clear() // ArrayBuffer()
If you want to convert your ArrayBuffer
to an immutable collection, you can do so easily:
val immutableSeq = buffer.toSeq val immutableList = buffer.toList
ArrayBuffer
is a versatile and commonly used mutable collection in Scala. It provides array-like performance characteristics (fast random access) while allowing dynamic resizing. However, being mutable, you should be careful when using it in a multi-threaded environment or when mutability can introduce bugs or make code harder to reason about.
Creating and Initializing ArrayBuffer in Scala:
ArrayBuffer
is a mutable, resizable array in Scala. You can create and initialize it with elements.
import scala.collection.mutable.ArrayBuffer val numbers: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
Adding and Removing Elements from ArrayBuffer:
You can add or remove elements from an ArrayBuffer
using various methods.
// Adding elements numbers += 6 numbers ++= Seq(7, 8) // Removing elements numbers -= 3 numbers.remove(1) println(numbers) // Result: ArrayBuffer(1, 4, 5, 6, 7, 8)
Iterating Over Elements in Scala ArrayBuffer:
You can iterate over the elements of an ArrayBuffer
using different iteration methods.
for (num <- numbers) { println(num) } // Or using foreach numbers.foreach(println)
Common Operations and Methods with ArrayBuffer:
ArrayBuffer
provides various methods for common operations like appending, prepending, finding, and sorting elements.
// Appending elements numbers.append(9, 10) // Prepending elements numbers.prepend(0, -1) // Finding elements val index = numbers.indexOf(6) // Sorting elements numbers.sorted println(numbers) // Result: ArrayBuffer(-1, 0, 1, 4, 5, 6, 7, 8, 9, 10)
Resizable Arrays in Scala with ArrayBuffer:
ArrayBuffer
provides resizable arrays, allowing you to dynamically adjust the size of the collection.
// Resize to a specific length numbers.trimEnd(2) // Increase the size numbers.trimEnd(15) numbers.sizeHint(20) println(numbers.length) // Result: 20
Converting ArrayBuffer to Other Collections in Scala:
You can convert an ArrayBuffer
to other collection types like List
, Seq
, or Array
.
val list: List[Int] = numbers.toList val seq: Seq[Int] = numbers.toSeq val array: Array[Int] = numbers.toArray
These conversions allow interoperability with different collection types.