Scala | ArrayBuffer
Last Updated :
05 Mar, 2019
Improve
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 // Scala program to create an 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" 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 // 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)) } }
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)
Scala // Scala program to add element in 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]() // 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 buffer
- Deleting ArrayBuffer Elements:
- Remove one element
ArrayBuffer-= (element)
- Remove multiple elements
ArrayBuffer-= (elem1, elem2, ....., elemN)
Scala // Scala program to delete element from 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( "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 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 // Scala program for remove method, on 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( "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)