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

  1. Creating instance of ArrayBuffer:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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)
    }
    }

    chevron_right

    
    

    Output:

    ArrayBuffer(GeeksForGeeks, gfg, Chandan)
    
  2. Access element from ArrayBuffer:
    Element is accessed same as array, ArrayBuffer(i) is used to accessed ith index element of array.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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)) 
    }
    }

    chevron_right

    
    

    Output:

    gfg
    
  3. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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) 
    }
    }

    chevron_right

    
    

    Output:

    ArrayBuffer(GeeksForGeeks, gfg, chandan, S-series, Ritesh)
    
  4. Deleting ArrayBuffer Elements:
    • Remove one element
      ArrayBuffer-= (element)
    • Remove multiple elements
      ArrayBuffer-= (elem1, elem2, ....., elemN)

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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) 
    }
    }

    chevron_right

    
    

    Output:

    ArrayBuffer(S-series, Ritesh)
    
  5. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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) 
    }
    }

    chevron_right

    
    

    Output:

    ArrayBuffer(gfg, chandan, S-series, Ritesh)
    ArrayBuffer(gfg)
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.