Skip to main content

How to add new values to an inmutable Array in Scala

The way of adding a new value into an existing array is:

var myArray: Array[String] = Array.empty

// To Append
myArray = myArray :+ "String appended"

// To Prepend
myArray = "String prepended" +: myArray

Note that the array needs to be declared as a “var”, as the append and prepend operations return a new array with the value added, so if you don’t want to create a “var”, you can always assign it to a new variable.