home search about rss feed twitter ko-fi

ArrayBuffer, Part 2 (Scala 3 Video)

ArrayBuffer, Part 2: Updating and Deleting Elements

The Scala ArrayBuffer is mutable, and this video shows a few common ways to update and delete elements in an ArrayBuffer.

How to update ArrayBuffer elements

This section shows examples of how to update ArrayBuffer elements.

Updating existing elements:

val a = (1 to 4).toBuffer             // ArrayBuffer(1, 2, 3, 4)
a(2) = 50                             // ArrayBuffer(1, 2, 50, 4)
a.update(0, 10)                       // ArrayBuffer(10, 2, 50, 4)
a.update(3, 40)                       // ArrayBuffer(10, 2, 50, 40)

Appending, prepending, and inserting

Update can also mean append, so here are a few ways to append elements with += and ++=:

val nums = ArrayBuffer(1, 2, 3)   # ArrayBuffer(1, 2, 3)
nums += 4                         # ArrayBuffer(1, 2, 3, 4)
nums += (5, 6)                    # ArrayBuffer(1, 2, 3, 4, 5, 6)
nums ++= List(7, 8)               # ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8)

With appending, inserting, and prepending you can use symbolic method names, or names like these:

  • append, appendAll
  • insert, insertAll
  • prepend, prependAll

The symbolic names are typically aliases for named methods like these.

More examples:

val a = ArrayBuffer(1, 2, 3)      // ArrayBuffer(1, 2, 3)
a.append(4)                       // ArrayBuffer(1, 2, 3, 4)
a.appendAll(Seq(5, 6))            // ArrayBuffer(1, 2, 3, 4, 5, 6)

a.clear                           // ArrayBuffer()

val a = ArrayBuffer(9, 10)        // ArrayBuffer(9, 10)
a.insert(0, 8)                    // ArrayBuffer(8, 9, 10)
a.insertAll(0, Vector(6, 7))      // ArrayBuffer(6, 7, 8, 9, 10)
a.prepend(5)                      // ArrayBuffer(5, 6, 7, 8, 9, 10)
a.prependAll(Seq(1,2,3,4))        // ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Updating in place

Because ArrayBuffer is mutable, you can update its elements in place:

// an example ArrayBuffer:
val a = ArrayBuffer.tabulate(10)(i => i + 1)  // a: ArrayBuffer(1 .. 10)

a.dropInPlace(1)             // ArrayBuffer(2, 3, 4, 5, 6, 7, 8, 9, 10)
a.dropRightInPlace(1)        // ArrayBuffer(2, 3, 4, 5, 6, 7, 8, 9)
a.dropWhileInPlace(_ < 4)    // ArrayBuffer(4, 5, 6, 7, 8, 9)
a.filterInPlace(_ > 4)       // ArrayBuffer(5, 6, 7, 8, 9)
a.mapInPlace(_ * 10)         // ArrayBuffer(50, 60, 70, 80, 90)
a.takeInPlace(4)             // ArrayBuffer(50, 60, 70, 80)
a.takeRightInPlace(3)        // ArrayBuffer(60, 70, 80)
a.takeWhileInPlace(_ < 80)   // ArrayBuffer(60, 70)

Deleting ArrayBuffer elements

Use methods and approaches like these to delete ArrayBuffer elements:

// -= and --=

val a = ArrayBuffer.range('a', 'h')   // ArrayBuffer(a, b, c, d, e, f, g)
a -= 'a'                              // ArrayBuffer(b, c, d, e, f, g)
a --= Seq('b', 'c')                   // ArrayBuffer(d, e, f, g)
a --= Set('d')                        // ArrayBuffer(e, f, g)


// remove

val a = ArrayBuffer.range('a', 'h')   // ArrayBuffer(a, b, c, d, e, f, g)
a.remove(0)                           // ArrayBuffer(b, c, d, e, f, g)
a.remove(2, 3)                        // ArrayBuffer(b, c, g)


// trimStart and trimEnd

val a = ArrayBuffer.range('a', 'h')   // ArrayBuffer(a, b, c, d, e, f, g)
a.trimStart(2)                        // ArrayBuffer(c, d, e, f, g)
a.trimEnd(2)                          // ArrayBuffer(c, d, e)


// clear

val a = ArrayBuffer.range('a', 'h')   // ArrayBuffer(a, b, c, d, e, f, g)
a.clear                               // ArrayBuffer[Char] = ArrayBuffer()

More details

For MANY more details and examples, see:

Update: All of my new videos are now on
LearnScala.dev