Scala | ArrayBuffer
Last Updated :
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.
Operations on ArrayBuffer
- Creating instance of ArrayBuffer:
import scala.collection.mutable.ArrayBuffer
object GFG
{
def main(args : Array[String])
{
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.
import scala.collection.mutable.ArrayBuffer
object GFG
{
def main(args : Array[String])
{
var name = ArrayBuffer[String]()
name + = "GeeksForGeeks"
name + = "gfg"
name + = "Chandan"
println(name( 1 ))
}
}
|
- 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:
import scala.collection.mutable.ArrayBuffer
object GFG
{
def main(args : Array[String])
{
var name = ArrayBuffer[String]()
name + = "GeeksForGeeks"
name + = ( "gfg" , "chandan" )
name.append( "S-series" , "Ritesh" )
println(name)
}
}
|
Output:
ArrayBuffer(GeeksForGeeks, gfg, chandan, S-series, Ritesh)
- Deleting ArrayBuffer Elements:
Example:
import scala.collection.mutable.ArrayBuffer
object GFG
{
def main(args : Array[String])
{
var name = ArrayBuffer( "GeeksForGeeks" , "gfg" ,
"chandan" , "S-series" ,
"Ritesh" )
name - = "GeeksForGeeks"
name - = ( "gfg" , "chandan" )
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:
import scala.collection.mutable.ArrayBuffer
object GFG
{
def main(args : Array[String])
{
var name = ArrayBuffer( "GeeksForGeeks" ,
"gfg" , "chandan" ,
"S-series" , "Ritesh" )
name.remove( 0 )
println(name)
name.remove( 1 , 3 )
println(name)
}
}
|
Output:
ArrayBuffer(gfg, chandan, S-series, Ritesh)
ArrayBuffer(gfg)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...