🌐
Scala Documentation
docs.scala-lang.org › overviews › collections › concrete-mutable-collection-classes.html
Concrete Mutable Collection Classes | Collections (Scala 2.8 - 2.12) | Scala Documentation
You’ve now seen the most commonly used immutable collection classes that Scala provides in its standard library. Take a look now at the mutable collection classes. An ArrayBuffer buffer holds an array and a size. Most operations on an array buffer have the same speed as for an array, because the operations simply access and modify the underlying array.
🌐
Alvin Alexander
alvinalexander.com › scala › scala-mutable-arrays-adding-elements-to-arrays
Mutable Scala arrays (adding elements to arrays) | alvinalexander.com
Just a quick note today that if you want to create a mutable Scala array — particularly an array that can grow in size after you first declare it — you need to use the Scala ArrayBuffer class instead of the Array class, which can’t grow.
🌐
Allaboutscala
allaboutscala.com › tutorials › chapter-7-beginner-tutorial-using-scala-mutable-collection › scala-tutorial-learn-use-mutable-array
Scala Tutorial - Learn How To Use Scala's Mutable Array
February 3, 2020 - In this tutorial, we will learn how to use Scala's Mutable Array to perform common operations such as initialize an Array, access elements at specific index, create 2D or 3D dimensional Arrays, and copy Arrays.
🌐
IncludeHelp
includehelp.com › scala › arraybuffer-in-scala-creating-mutable-arrays.aspx
ArrayBuffer in Scala (Creating Mutable Arrays)
October 20, 2024 - You can add new element(s) to the ArrayBuffer using the += operator and using the append() method. import scala.collection.mutable.ArrayBuffer object MyClass { def main(args: Array[String]) { println("My bikes are ") val bikes = ArrayBuffer[String]() bikes += "ThunderBird 350" bikes += ("YRF R3", "Iron 883") bikes.append("BMW S1000 RR") for(i <- 0 to bikes.length-1) println(bikes(i)) } }
🌐
GeeksforGeeks
geeksforgeeks.org › scala › scala-arraybuffer
Scala | ArrayBuffer - GeeksforGeeks
March 5, 2019 - To create a mutable, indexed sequence whose size can change ArrayBuffer class is used. To use, ArrayBuffer, scala.collection.mutable.ArrayBuffer class is imported, an instance of ArrayBuffer is created.
🌐
Scala Programming Language
scala-lang.org › api › 2.13.3 › scala › collection › mutable › ArrayBuffer.html
Scala Standard Library 2.13.3 - scala.collection.mutable.ArrayBuffer
scala.collection.mutable · An implementation of the Buffer class using an array to represent the assembled sequence internally. Append, update and random access take constant time (amortized time). Prepends and removes are linear in the buffer size. A · the type of this arraybuffer's elements.
🌐
Scala Documentation
docs.scala-lang.org › overviews › collections-2.13 › concrete-mutable-collection-classes.html
Concrete Mutable Collection Classes | Collections | Scala Documentation
You’ve now seen the most commonly used immutable collection classes that Scala provides in its standard library. Take a look now at the mutable collection classes. An ArrayBuffer holds an array and a size. Most operations on an array buffer have the same speed as for an array, because the operations simply access and modify the underlying array.
🌐
Handsonscala
handsonscala.com › chapter-4-scala-collections.html
Hands-on Scala: Scala Collections
While Arrays are the low-level primitive, most Scala applications are built upon its mutable and immutable collections: Vectors, Lists, Sets, and Maps.
🌐
O'Reilly
oreilly.com › library › view › scala-cookbook › 9781449340292 › ch11s09.html
11.8. Creating an Array Whose Size Can Change (ArrayBuffer) - Scala Cookbook [Book]
August 13, 2013 - To create a mutable, indexed sequence whose size can change, use the ArrayBuffer class. To use an ArrayBuffer, import it into scope and then create an instance. You can declare an ArrayBuffer without initial elements, and then add them later: ...
Author: Alvin Alexander
Published: 2013
Pages: 720
Find elsewhere
🌐
Allaboutscala
allaboutscala.com › tutorials › chapter-7-beginner-tutorial-using-scala-mutable-collection › scala-tutorial-learn-use-mutable-arraybuffer
Scala Tutorial - Learn How To Use Scala's Mutable ArrayBuffer
February 3, 2020 - In this tutorial, we will learn how to use Scala's Mutable ArrayBuffer to perform common operations such as initialize an ArrayBuffer, access elements at specific index, add and remove elements and create an empty ArrayBuffer.
Top answer
1 of 8
32

You could cast your array into a sequence.

val s: Seq[Int] = Array(1,2,3,4)

The array will be implicitly converted to a WrappedArray. And as the type is Seq, update operations will no longer be available.

2 of 8
21

So, let's first make a distinction between interface and class. The interface is an API design, while the class is the implementation of such API.

The interfaces in Scala have the same name and different package to distinguish with regards to immutability: Seq, immutable.Seq, mutable.Seq.

The classes, on the other hand, usually don't share a name. A List is an immutable sequence, while a ListBuffer is a mutable sequence. There are exceptions, like HashSet, but that's just a coincidence with regards to implementation.

Now, and Array is not part of Scala's collection, being a Java class, but its wrapper WrappedArray shows clearly where it would show up: as a mutable class.

The interface implemented by WrappedArray is IndexedSeq, which exists are both mutable and immutable traits.

The immutable.IndexedSeq has a few implementing classes, including the WrappedString. The general use class implementing it, however, is the Vector. That class occupies the same position an Array class would occupy in the mutable side.

Now, there's no more complexity in using a Vector than using an Array, so I don't know why you call it complicated.

Perhaps you think it does too much internally, in which case you'd be wrong. All well designed immutable classes are persistent, because using an immutable collection means creating new copies of it, so they have to be optimized for that, which is exactly what Vector does.

🌐
Scala Programming Language
scala-lang.org › api › current › scala › collection › mutable › ArrayBuffer.html
ArrayBuffer
An implementation of the Buffer class using an array to represent the assembled sequence internally. Append, update and random access take constant time (amortized time).
🌐
Scala Programming Language
scala-lang.org › api › 2.12.3 › scala › collection › mutable › ArrayBuffer.html
Scala Standard Library 2.12.3 - scala.collection.mutable.ArrayBuffer
The scala package contains core types like Int, Float, Array or Option which are accessible in all Scala compilation units without explicit qualification or imports. ... scala.collection.immutable - Immutable, sequential data-structures such as Vector, List, Range, HashMap or HashSet · scala.collection.mutable - Mutable, sequential data-structures such as ArrayBuffer, StringBuilder, HashMap or HashSet
🌐
Educative
educative.io › answers › what-are-concrete-mutable-collection-classes-in-scala
What are concrete mutable collection classes in Scala?
... Let’s cover some important mutable collection classes in this shot. An ArrayBuffer in Scala is used to build large collections of data. It is used to hold an array along with the size of the array.
🌐
Baeldung
baeldung.com › home › scala collections › guide to arraybuffer
Guide to ArrayBuffer | Baeldung on Scala
March 18, 2024 - Another important detail is that the ArrayBuffer implementation in specific is backed up by an indexed array, meaning that we have constant access to any element of the collection. ... scala> import scala.collection.mutable.ArrayBuffer scala> val nums = ArrayBuffer(1, 2, 3) val nums: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
🌐
Scaler
scaler.com › home › topics › scala › array in scala
Array In Scala - Scaler Topics
September 7, 2023 - Array in Scala is a mutable, fixed-size collection that stores elements of the same type.
Top answer
1 of 4
36

Depends what you need.

DoubleLinkedList is a linked list which allows you to traverse back-and-forth through the list of nodes. Use its prev and next references to go to the previous or the next node, respectively.

LinkedList is a singly linked list, so there are not prev pointers - if you only traverse to the next element of the list all the time, this is what you need.

EDIT: Note that the two above are meant to be used internally as building blocks for more complicated list structures like MutableLists which support efficient append, and mutable.Queues.

The two collections above both have linear-time append operations.

ListBuffer is a buffer class. Although it is backed by a singly linked list data structure, it does not expose the next pointer to the client, so you can only traverse it using iterators and the foreach. Its main use is, however, as a buffer and an immutable list builder - you append elements to it via +=, and when you call result, you very efficiently get back a functional immutable.List. Unlike mutable and immutable lists, both append and prepend operations are constant-time - you can append at the end via += very efficiently.

MutableList is used internally, you usually do not use it unless you plan to implement a custom collection class based on the singly linked list data structure. Mutable queues, for example, inherit this class. MutableList class also has an efficient constant-time append operation, because it maintains a reference to the last node in the list.

2 of 4
22

The documentation's Concrete Mutable Collection Classes page (or the one for 2.12) has an overview of mutable list classes, including explanations on when to use which one.

🌐
University of Helsinki
cs.helsinki.fi › u › wikla › OTS › Sisalto › examples › html › ch17.html
Code Examples for Programming in Scala
scala> List(1, 2, 3) res0: List[Int] = List(1, 2, 3) scala> Set('a', 'b', 'c') res1: scala.collection.immutable.Set[Char] = Set(a, b, c) scala> import scala.collection.mutable import scala.collection.mutable scala> mutable.Map("hi" -> 2, "there" -> 5) res2: scala.collection.mutable.Map[java.lang.String,Int] = Map(hi -> 2, there -> 5) scala> Array(1.0, 2.0, 3.0) res3: Array[Double] = Array(1.0, 2.0, 3.0)