Scala | ArrayBuffer

05 Mar, 2019
Array in scala is homogeneous and mutable, i.e it contains elements of the same data type and its elements can change but the size of array size can’t change. 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.Internally, an ArrayBuffer is an Array of elements, as well as the store’s current size of the array. When an element is added to an ArrayBuffer, this size is checked. If the underlying array isn’t full, then the element is directly added to the array. If the underlying array is full, then a larger array is constructed and all the elements are copied to the new array. The key is that the new array is constructed larger than required for the current addition.
Example:
var name = new ArrayBuffer[datatype]()  // empty buffer is created

var name = new ArrayBuffer("chandan", "gfg", "geeksforgeeks")

In above example, first, an empty buffer is created here datatype indicates the type of data such as integer, string. Then created buffer with three elements, of type string.
Creating instance of ArrayBuffer:// Scala program to create an ArrayBuffer// ArrayBuffer class is importedimport scala.collection.mutable.ArrayBuffer  // Creating Objectobject GFG {      // Main Methoddef main(args: Array[String]){    // Instance of ArrayBuffer is created    var name = ArrayBuffer[String]()     name += "GeeksForGeeks"    name += "gfg"    name += "Chandan"    println(name)}}Output:ArrayBuffer(GeeksForGeeks, gfg, Chandan)
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 importedimport scala.collection.mutable.ArrayBuffer  // Creating Objectobject GFG {      // Main Methoddef 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)) }}Output:gfg
Adding elements in ArrayBuffer:Add single element to the bufferArrayBuffer+=( element)Add two or more elements (method has a varargs parameter)ArrayBuffer+= (element1, element2, ..., elementN )Append one or more elements (uses a varargs parameter)ArrayBuffer.append( elem1, elem2, ... elemN)Example:// Scala program to add element in ArrayBuffer// ArrayBuffer class is importedimport scala.collection.mutable.ArrayBuffer  // Creating Objectobject GFG{      // Main Methoddef main(args: Array[String]) {    // Instance of ArrayBuffer is created    var name = ArrayBuffer[String]()       // adding one element    name += "GeeksForGeeks"              // add two or more elements     name += ("gfg", "chandan")              // adding one or more element using append method     name.append("S-series", "Ritesh")           // printing arraybuffer    println(name) }}Output:ArrayBuffer(GeeksForGeeks, gfg, chandan, S-series, Ritesh)
Deleting ArrayBuffer Elements:Remove one elementArrayBuffer-= (element)Remove multiple elementsArrayBuffer-= (elem1, elem2, ....., elemN)Example:// Scala program to delete element from ArrayBuffer// ArrayBuffer class is importedimport scala.collection.mutable.ArrayBuffer  // Creating Objectobject GFG {      // Main Methoddef main(args: Array[String]) {    // Instance of ArrayBuffer is created    var name = ArrayBuffer( "GeeksForGeeks","gfg",                            "chandan","S-series",                             "Ritesh" )       // deletes one element    name -= "GeeksForGeeks"              // deletes two or more elements     name -= ("gfg", "chandan")          // printing resultant arraybuffer    println(name) }}Output:ArrayBuffer(S-series, Ritesh)
Deleting ArrayBuffer Elements using ArrayBuffer.remove() :The remove method is used to delete one element by its position in the ArrayBuffer, or a series of elements beginning at a starting position.Example:// Scala program for remove method, on ArrayBuffer// ArrayBuffer class is importedimport scala.collection.mutable.ArrayBuffer  // Creating Objectobject GFG {      // Main Methoddef main(args: Array[String]) {    // Instance of ArrayBuffer is created    var name = ArrayBuffer( "GeeksForGeeks",                            "gfg", "chandan",                            "S-series", "Ritesh" )           // removing 0th index element    name.remove(0)           // printing resultant arraybuffer    println(name)    name.remove(1, 3)          // printing resultant arraybuffer    println(name) }}Output:ArrayBuffer(gfg, chandan, S-series, Ritesh)
ArrayBuffer(gfg)

Creating instance of ArrayBuffer:// Scala program to create an ArrayBuffer// ArrayBuffer class is importedimport scala.collection.mutable.ArrayBuffer  // Creating Objectobject GFG {      // Main Methoddef main(args: Array[String]){    // Instance of ArrayBuffer is created    var name = ArrayBuffer[String]()     name += "GeeksForGeeks"    name += "gfg"    name += "Chandan"    println(name)}}Output:ArrayBuffer(GeeksForGeeks, gfg, Chandan)

// Scala program to create an ArrayBuffer// ArrayBuffer class is importedimport scala.collection.mutable.ArrayBuffer  // Creating Objectobject GFG {      // Main Methoddef main(args: Array[String]){    // Instance of ArrayBuffer is created    var name = ArrayBuffer[String]()     name += "GeeksForGeeks"    name += "gfg"    name += "Chandan"    println(name)}}
ArrayBuffer(GeeksForGeeks, gfg, Chandan)

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 importedimport scala.collection.mutable.ArrayBuffer  // Creating Objectobject GFG {      // Main Methoddef 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)) }}Output:gfg

// Scala program to access element of ArrayBuffer// ArrayBuffer class is importedimport scala.collection.mutable.ArrayBuffer  // Creating Objectobject GFG {      // Main Methoddef 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)) }}
gfg

Adding elements in ArrayBuffer:Add single element to the bufferArrayBuffer+=( element)Add two or more elements (method has a varargs parameter)ArrayBuffer+= (element1, element2, ..., elementN )Append one or more elements (uses a varargs parameter)ArrayBuffer.append( elem1, elem2, ... elemN)Example:// Scala program to add element in ArrayBuffer// ArrayBuffer class is importedimport scala.collection.mutable.ArrayBuffer  // Creating Objectobject GFG{      // Main Methoddef main(args: Array[String]) {    // Instance of ArrayBuffer is created    var name = ArrayBuffer[String]()       // adding one element    name += "GeeksForGeeks"              // add two or more elements     name += ("gfg", "chandan")              // adding one or more element using append method     name.append("S-series", "Ritesh")           // printing arraybuffer    println(name) }}Output:ArrayBuffer(GeeksForGeeks, gfg, chandan, S-series, Ritesh)

Add single element to the bufferArrayBuffer+=( element)
ArrayBuffer+=( element)
Add two or more elements (method has a varargs parameter)ArrayBuffer+= (element1, element2, ..., elementN )
ArrayBuffer+= (element1, element2, ..., elementN )
Append one or more elements (uses a varargs parameter)ArrayBuffer.append( elem1, elem2, ... elemN)
ArrayBuffer.append( elem1, elem2, ... elemN)
Example:
// Scala program to add element in ArrayBuffer// ArrayBuffer class is importedimport scala.collection.mutable.ArrayBuffer  // Creating Objectobject GFG{      // Main Methoddef main(args: Array[String]) {    // Instance of ArrayBuffer is created    var name = ArrayBuffer[String]()       // adding one element    name += "GeeksForGeeks"              // add two or more elements     name += ("gfg", "chandan")              // adding one or more element using append method     name.append("S-series", "Ritesh")           // printing arraybuffer    println(name) }}
ArrayBuffer(GeeksForGeeks, gfg, chandan, S-series, Ritesh)

Deleting ArrayBuffer Elements:Remove one elementArrayBuffer-= (element)Remove multiple elementsArrayBuffer-= (elem1, elem2, ....., elemN)Example:// Scala program to delete element from ArrayBuffer// ArrayBuffer class is importedimport scala.collection.mutable.ArrayBuffer  // Creating Objectobject GFG {      // Main Methoddef main(args: Array[String]) {    // Instance of ArrayBuffer is created    var name = ArrayBuffer( "GeeksForGeeks","gfg",                            "chandan","S-series",                             "Ritesh" )       // deletes one element    name -= "GeeksForGeeks"              // deletes two or more elements     name -= ("gfg", "chandan")          // printing resultant arraybuffer    println(name) }}Output:ArrayBuffer(S-series, Ritesh)

Remove one elementArrayBuffer-= (element)
ArrayBuffer-= (element)
Remove multiple elementsArrayBuffer-= (elem1, elem2, ....., elemN)
ArrayBuffer-= (elem1, elem2, ....., elemN)
Example:
// Scala program to delete element from ArrayBuffer// ArrayBuffer class is importedimport scala.collection.mutable.ArrayBuffer  // Creating Objectobject GFG {      // Main Methoddef main(args: Array[String]) {    // Instance of ArrayBuffer is created    var name = ArrayBuffer( "GeeksForGeeks","gfg",                            "chandan","S-series",                             "Ritesh" )       // deletes one element    name -= "GeeksForGeeks"              // deletes two or more elements     name -= ("gfg", "chandan")          // printing resultant arraybuffer    println(name) }}
ArrayBuffer(S-series, Ritesh)

Deleting ArrayBuffer Elements using ArrayBuffer.remove() :The remove method is used to delete one element by its position in the ArrayBuffer, or a series of elements beginning at a starting position.Example:// Scala program for remove method, on ArrayBuffer// ArrayBuffer class is importedimport scala.collection.mutable.ArrayBuffer  // Creating Objectobject GFG {      // Main Methoddef main(args: Array[String]) {    // Instance of ArrayBuffer is created    var name = ArrayBuffer( "GeeksForGeeks",                            "gfg", "chandan",                            "S-series", "Ritesh" )           // removing 0th index element    name.remove(0)           // printing resultant arraybuffer    println(name)    name.remove(1, 3)          // printing resultant arraybuffer    println(name) }}Output:ArrayBuffer(gfg, chandan, S-series, Ritesh)
ArrayBuffer(gfg)

The remove method is used to delete one element by its position in the ArrayBuffer, or a series of elements beginning at a starting position.
Example:
// Scala program for remove method, on ArrayBuffer// ArrayBuffer class is importedimport scala.collection.mutable.ArrayBuffer  // Creating Objectobject GFG {      // Main Methoddef main(args: Array[String]) {    // Instance of ArrayBuffer is created    var name = ArrayBuffer( "GeeksForGeeks",                            "gfg", "chandan",                            "S-series", "Ritesh" )           // removing 0th index element    name.remove(0)           // printing resultant arraybuffer    println(name)    name.remove(1, 3)          // printing resultant arraybuffer    println(name) }}
ArrayBuffer(gfg, chandan, S-series, Ritesh)
ArrayBuffer(gfg)

Scala
Scala-Arrays
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
For Loop in Scala
Scala | map() method
Scala | flatMap Method
String concatenation in Scala
Scala | reduce() Function
Type Casting in Scala
Scala List filter() method with example
Scala Tutorial – Learn Scala with Step By Step Guide
Scala String substring() method with example
Enumeration in Scala