How to delete Array and ArrayBuffer elements in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.9, “How to Delete Array and ArrayBuffer Elements in Scala”

Problem

You want to delete elements from an Array or ArrayBuffer.

Solution

An ArrayBuffer is a mutable sequence, so you can delete elements with the usual -=, --=, remove, and clear methods.

You can remove one or more elements with -=:

import scala.collection.mutable.ArrayBuffer

val x = ArrayBuffer('a', 'b', 'c', 'd', 'e')

// remove one element
x -= 'a'

// remove multiple elements (methods defines a varargs param)
x -= ('b', 'c')

Use --= to remove multiple elements that are declared in another collection (any collection that extends TraversableOnce):

val x = ArrayBuffer('a', 'b', 'c', 'd', 'e')
x --= Seq('a', 'b')
x --= Array('c')
x --= Set('d')

Use the remove method to delete one element by its position in the ArrayBuffer, or a series of elements beginning at a starting position:

scala> val x = ArrayBuffer('a', 'b', 'c', 'd', 'e', 'f')
x: scala.collection.mutable.ArrayBuffer[Char] = ArrayBuffer(a, b, c, d, e, f)

scala> x.remove(0)
res0: Char = a

scala> x
res1: scala.collection.mutable.ArrayBuffer[Char] = ArrayBuffer(b, c, d, e, f)

scala> x.remove(1, 3)

scala> x
res2: scala.collection.mutable.ArrayBuffer[Char] = ArrayBuffer(b, f)

In these examples, the collection that contains the elements to be removed can be any collection that extends TraversableOnce, so removeThese can be a Seq, Array, Vector, and many other types that extend TraversableOnce.

The clear method removes all the elements from an ArrayBuffer:

scala> var a = ArrayBuffer(1,2,3,4,5)
a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)

scala> a.clear

scala> a
res0: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

You can also use the usual Scala filtering methods (drop, filter, take, etc.) to filter elements out of a collection; just remember to assign the result to a new variable.

Array

The size of an Array can’t be changed, so you can’t directly delete elements. You can reassign the elements in an Array, which has the effect of replacing them:

scala> val a = Array("apple", "banana", "cherry")
a: Array[String] = Array(apple, banana, cherry)

scala> a(0) = ""

scala> a(1) = null

scala> a
res0: Array[String] = Array("", null, cherry)

You can also filter elements out of one array while you assign the result to a new array:

scala> val a = Array("apple", "banana", "cherry")
a: Array[String] = Array(apple, banana, cherry)

scala> val b = a.filter(! _.contains("apple"))
b: Array[String] = Array(banana, cherry)

Use other filtering methods (drop, slice, take, etc.) in the same way.

If you define the array variable as a var, you can assign the result back to itself, which gives the appearance of deleting elements using filtering:

scala> var a = Array("apple", "banana", "cherry")
a: Array[String] = Array(apple, banana, cherry)

scala> a = a.take(2)
a: Array[String] = [LString;@e41a882

scala> a
res0: Array[String] = Array(apple, banana)

The Scala Cookbook

This tutorial is sponsored by the Scala Cookbook, which I wrote for O’Reilly:

You can find the Scala Cookbook at these locations:

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.