Scala | ArrayBuffer
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.
Operations on ArrayBuffer
- Creating instance of ArrayBuffer:
// Scala program to create an ArrayBuffer// ArrayBuffer class is importedimportscala.collection.mutable.ArrayBuffer// Creating ObjectobjectGFG{// Main Methoddefmain(args:Array[String]){// Instance of ArrayBuffer is createdvarname=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 importedimportscala.collection.mutable.ArrayBuffer// Creating ObjectobjectGFG{// Main Methoddefmain(args:Array[String]){// Instance of ArrayBuffer is createdvarname=ArrayBuffer[String]()name +="GeeksForGeeks"name +="gfg"name +="Chandan"// accessing 1th index element of arraybufferprintln(name(1))}}Output:gfg
- Adding elements in ArrayBuffer:
- Add single element to the buffer
ArrayBuffer+=( 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 importedimportscala.collection.mutable.ArrayBuffer// Creating ObjectobjectGFG{// Main Methoddefmain(args:Array[String]){// Instance of ArrayBuffer is createdvarname=ArrayBuffer[String]()// adding one elementname +="GeeksForGeeks"// add two or more elementsname +=("gfg","chandan")// adding one or more element using append methodname.append("S-series","Ritesh")// printing arraybufferprintln(name)}}Output:ArrayBuffer(GeeksForGeeks, gfg, chandan, S-series, Ritesh)
- Add single element to the buffer
- Deleting ArrayBuffer Elements:
- Remove one element
ArrayBuffer-= (element)
- Remove multiple elements
ArrayBuffer-= (elem1, elem2, ....., elemN)
Example:
// Scala program to delete element from ArrayBuffer// ArrayBuffer class is importedimportscala.collection.mutable.ArrayBuffer// Creating ObjectobjectGFG{// Main Methoddefmain(args:Array[String]){// Instance of ArrayBuffer is createdvarname=ArrayBuffer("GeeksForGeeks","gfg","chandan","S-series","Ritesh")// deletes one elementname -="GeeksForGeeks"// deletes two or more elementsname -=("gfg","chandan")// printing resultant arraybufferprintln(name)}}Output:ArrayBuffer(S-series, Ritesh)
- Remove one element
- 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 importedimportscala.collection.mutable.ArrayBuffer// Creating ObjectobjectGFG{// Main Methoddefmain(args:Array[String]){// Instance of ArrayBuffer is createdvarname=ArrayBuffer("GeeksForGeeks","gfg","chandan","S-series","Ritesh")// removing 0th index elementname.remove(0)// printing resultant arraybufferprintln(name)name.remove(1,3)// printing resultant arraybufferprintln(name)}}Output:ArrayBuffer(gfg, chandan, S-series, Ritesh) ArrayBuffer(gfg)


