🌐
Scala Documentation
docs.scala-lang.org › overviews › scala-book › arraybuffer-examples.html
The ArrayBuffer Class | Scala Book | Scala Documentation
You remove elements from an ArrayBuffer with the -= and --= methods: // remove one element nums -= 9 // remove multiple elements nums -= 7 -= 8 // remove multiple elements using another collection nums --= Array(5, 6) Here’s what all of those examples look like in the REPL: scala> import scala.collection.mutable.ArrayBuffer scala> val nums = ArrayBuffer(1, 2, 3) val nums: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3) scala> nums += 4 val res0: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4) scala> nums += 5 += 6 val res1: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6) scala> nums ++= List(7, 8, 9) va
🌐
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
Core Scala types. They are always available without an explicit import. ... 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.
🌐
Scala Programming Language
scala-lang.org › api › current › scala › collection › mutable › ArrayBuffer.html
ArrayBuffer
class ArrayBuffer[A] extends AbstractBuffer[A], IndexedBuffer[A], IndexedSeqOps[A, ArrayBuffer, ArrayBuffer[A]], StrictOptimizedSeqOps[A, ArrayBuffer, ArrayBuffer[A]], IterableFactoryDefaults[A, ArrayBuffer], DefaultSerializable
🌐
TutorialsPoint
tutorialspoint.com › scala_collections › scala_collections_arraybuffer.htm
Scala Collections - ArrayBuffer
Scala provides a data structure, the ArrayBuffer, which can change size when initial size falls short. As array is of fix size and more elements cannot be occupied in an array, ArrayBuffer is an alternative to array where size is flexible.
🌐
Baeldung
baeldung.com › home › scala collections › guide to arraybuffer
Guide to ArrayBuffer | Baeldung on Scala
March 18, 2024 - ArrayBuffer allows us to insert a new element both at the beginning and end of the list. Furthermore, since it’s an indexed list, it’s also easy to insert an element in any position of the list.
🌐
Alvin Alexander
alvinalexander.com › scala › arraybuffer-class-methods-syntax-examples-reference
Scala ArrayBuffer class: methods, syntax, and examples | alvinalexander.com
ArrayBuffer is an indexed sequential collection. Use ListBuffer if you prefer a linear sequential collection that is mutable. The following examples are demonstrated in what I call a “CRUD+” order, meaning that the methods are shown in this order: ... In Scala, immutable collections are ...
🌐
GeeksforGeeks
geeksforgeeks.org › scala › scala-arraybuffer
Scala | ArrayBuffer - GeeksforGeeks
March 5, 2019 - Access element from ArrayBuffer: Element is accessed same as array, ArrayBuffer(i) is used to accessed ith index element of array. ... // Scala program to access element of ArrayBuffer // ArrayBuffer class is imported import scala.collection.mutable.ArrayBuffer // Creating Object object GFG { // Main Method def main(args: Array[String]) { // Instance of ArrayBuffer is created var name = ArrayBuffer[String]() name += "GeeksForGeeks" name += "gfg" name += "Chandan" // accessing 1th index element of arraybuffer println(name(1)) } }
🌐
Otfried
otfried.org › scala › arraybuffer.html
Scala Documentation -- ArrayBuffer
An ArrayBuffer object stores an array and its current length. It supports efficient append operations. You can add elements using +=. When you are done, the toArray method returns the contents as a normal array. scala> val A = scala.collection.mutable.ArrayBuffer.empty[Int] A: scala.collec...
🌐
GitHub
github.com › scala › docs.scala-lang › blob › main › _overviews › scala-book › arraybuffer-examples.md
docs.scala-lang/_overviews/scala-book/arraybuffer-examples.md at main · scala/docs.scala-lang
You remove elements from an ArrayBuffer with the -= and --= methods: // remove one element nums -= 9 // remove multiple elements nums -= 7 -= 8 // remove multiple elements using another collection nums --= Array(5, 6) Here’s what all of those examples look like in the REPL: scala> import scala.collection.mutable.ArrayBuffer scala> val nums = ArrayBuffer(1, 2, 3) val nums: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3) scala> nums += 4 val res0: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4) scala> nums += 5 += 6 val res1: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6) scala> nums ++= List(7, 8, 9) va
Author: scala
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 - As per the Scala Documentation, an ArrayBuffer is a mutable data structure which allows you to access and modify elements at specific index.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › scala tutorial › scala arraybuffer
Scala ArrayBuffer | Learn the Working for ArrayBuffer in Scala
June 6, 2023 - Scala ArrayBuffer is an indexed Sequence mutable data structure that allows us to add or change elements at a specific index. In ArrayBuffer we need not worry about the size; the size of an array buffer can be changed.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
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)) } }
Top answer
1 of 4
49

Both Array and ArrayBuffer are mutable, which means that you can modify elements at particular indexes: a(i) = e

ArrayBuffer is resizable, Array isn't. If you append an element to an ArrayBuffer, it gets larger. If you try to append an element to an Array, you get a new array. Therefore to use Arrays efficiently, you must know its size beforehand.

Arrays are implemented on JVM level and are the only non-erased generic type. This means that they are the most efficient way to store sequences of objects – no extra memory overhead, and some operations are implemented as single JVM opcodes.

ArrayBuffer is implemented by having an Array internally, and allocating a new one if needed. Appending is usually fast, unless it hits a limit and resizes the array – but it does it in such a way, that the overall effect is negligible, so don't worry. Prepending is implemented as moving all elements to the right and setting the new one as the 0th element and it's therefore slow. Appending n elements in a loop is efficient (O(n)), prepending them is not (O(n²)).

Arrays are specialized for built-in value types (except Unit), so Array[Int] is going to be much more optimal than ArrayBuffer[Int] – the values won't have to be boxed, therefore using less memory and less indirection. Note that the specialization, as always, works only if the type is monomorphic – Array[T] will be always boxed.

2 of 4
4

The one other difference is, Array's element created as on when its declared but Array Buffer's elements not created unless you assign values for the first time.

For example. You can write Array1(0)="Stackoverflow" but not ArrayBuffer1(0)="Stackoverflow" for the first time value assignments.

(Array1 = Array variable & ArrayBuffer1 = ArrayBuffer variable)

Because as we know, Array buffers are re-sizable, so elements created when you insert values at the first time and then you can modify/reassign them at the particular element.

Array:

Declaring and assigning values to Int Array.

val favNums= new ArrayInt

for(i<-0 to 19){
favNums(i)=i*2
}
favNums.foreach(println)

ArrayBuffer:

Declaring and assigning values to Int ArrayBuffer.

val favNumsArrayBuffer= new ArrayBuffer[Int]
    for(j<-0 to 19){
    favNumsArrayBuffer.insert(j, (j*2))
    //favNumsArrayBuffer++=Array(j*3)
      }
    favNumsArrayBuffer.foreach(println)

If you include favNumsArrayBuffer(j)=j*2 at the first line in the for loop, It doesn't work. But it works fine if you declare it in 2nd or 3rd line of the loop. Because values assigned already at the first line now you can modify by element index.

This simple one-hour video tutorial explains a lot.

https://youtu.be/DzFt0YkZo8M?t=2005

🌐
Reintech
reintech.io › blog › understanding-arraybuffer-in-scala
Understanding the ArrayBuffer in Scala | Reintech media
September 29, 2023 - Think of ArrayBuffer as Scala's answer to Java's ArrayList or Python's list—a workhorse collection for when you need mutability and performance.
🌐
GitHub
github.com › scala › scala › blob › v2.13.10 › src › library › scala › collection › mutable › ArrayBuffer.scala
scala/src/library/scala/collection/mutable/ArrayBuffer.scala at v2.13.10 · scala/scala
import scala.collection.convert.impl._ shape.parUnbox(new ObjectArrayStepper(array, 0, length).asInstanceOf[AnyStepper[A] with EfficientSplit]) } · override def knownSize: Int = super[IndexedSeqOps].knownSize · · /** Ensure that the internal array has at least `n` cells. */ protected def ensureSize(n: Int): Unit = { array = ArrayBuffer.ensureSize(array, size0, n) } ·
Author: scala
🌐
Scala Documentation
docs.scala-lang.org › overviews › collections-2.13 › concrete-mutable-collection-classes.html
Concrete Mutable Collection Classes | Collections | Scala Documentation
scala> val buf = scala.collection.mutable.ArrayBuffer.empty[Int] buf: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer() scala> buf += 1 res32: buf.type = ArrayBuffer(1) scala> buf += 10 res33: buf.type = ArrayBuffer(1, 10) scala> buf.toArray res34: Array[Int] = Array(1, 10)