← Back to Scala Course | Chapter 6: Collections Basics | Lesson 4 of 8

ArrayBuffer

In this page:

  1. ArrayBuffer

ArrayBuffer

ArrayBuffer (from scala.collection.mutable) is a resizable, mutable sequence, similar to a dynamic array or ArrayList in other languages. Unlike Array, it can grow or shrink with += and -= in addition to allowing element mutation. It's the go-to mutable collection when you need to build up a sequence incrementally.

Note: Use ArrayBuffer instead of Array when the collection's size needs to change after creation, not just its element values.

Example: ArrayBuffer

markup
import scala.collection.mutable.ArrayBuffer

object Main extends App {
  val buffer = ArrayBuffer(1, 2, 3)
  buffer += 4
  buffer += 5

  println(buffer)
  println(buffer.length)
}
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.