ArrayBuffer in Scala (Creating Mutable Arrays)

Scala | ArrayBuffer: In this tutorial, we are going to learn about the ArrayBuffers in Scala which is a mutable array. We will also learn to add elements to the ArrayBuffer.
Submitted by Shivang Yadav, on May 05, 2020

ArrayBuffer In Scala

In Scala, arrays are immutable and contain homogenous elements i.e. the size of the array cannot be changed and all the elements of the array contain the same elements.

ArrayBuffer is a special class the is used to create a mutable array.

To use the ArrayBuffer we will import,

    scala.collection.mutable.ArrayBuffer

Syntax:

Syntax to create an ArrayBuffer in Scala,

    var arrayBuffer = ArrayBuffer("element1", "element2", ...)

Program to create an ArrayBuffer

import scala.collection.mutable.ArrayBuffer

object MyClass {
    def main(args: Array[String]) {
        println("My bikes are ")
        val bikes = ArrayBuffer("ThunderBird 350", "YRF R3")
        for(i <- 0 to bikes.length-1)
            println(bikes(i))

        // adding a string 
        bikes += "iron 883"
        println("After adding new bike to my collection")
        for(i <- 0 to bikes.length-1)
            println(bikes(i))
        
    }
}

Output

My bikes are 
ThunderBird 350
YRF R3
After adding new bike to my collection
ThunderBird 350
YRF R3
iron 883

You can add new element(s) to the ArrayBuffer using the += operator and using the append() method.

Adding new elements to ArrayBuffer

import scala.collection.mutable.ArrayBuffer

object MyClass {
    def main(args: Array[String]) {
        println("My bikes are ")
        val bikes = ArrayBuffer[String]()
        
        bikes += "ThunderBird 350"
        bikes += ("YRF R3", "Iron 883")
        bikes.append("BMW S1000 RR")
        
        for(i <- 0 to bikes.length-1)
            println(bikes(i))
    }
}

Output

My bikes are 
ThunderBird 350
YRF R3
Iron 883
BMW S1000 RR

Deleting array elements

You can also delete elements of the ArrayBuffer using -= operator and ArrayBuffer.remove() method.

Syntax:

Deleting one element using -= operator

    ArrayBuffer -= (arrayElement)

Deleting multiple-elements using -= operator

    ArrayBuffer -= (arrayElement1, arrayElement2, ...)

Deleting element using remove method

    ArrayBuffer -= (arrayElementposition)

Program to delete elements of ArrayBuffer

import scala.collection.mutable.ArrayBuffer

object MyClass {
    def main(args: Array[String]) {
        val bikes = ArrayBuffer("ThunderBird 350", "YRF R3", "Iron 883", "BMW S1000 RR", "BMW GSA")
        println("My bikes are ")
        for(i <- 0 to bikes.length-1)
            println(bikes(i))
            
        println("Removing elements...")   
        bikes -= "YRF R3"
        bikes -= ("Iron 883", "BMW GSA")
        bikes.remove(1)
        for(i <- 0 to bikes.length-1)
            println(bikes(i))
    }
}

Output

My bikes are 
ThunderBird 350
YRF R3
Iron 883
BMW S1000 RR
BMW GSA
Removing elements...
ThunderBird 350

These were some operations on ArrayBuffer which is a special class that creates an Array mutable.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.