×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

ArrayBuffer in Scala (Creating Mutable Arrays)

By IncludeHelp Last updated : October 20, 2024

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.

Import Statement

To use the ArrayBuffer we will import,

scala.collection.mutable.ArrayBuffer

Creating an ArrayBuffer

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

Adding new elements to ArrayBuffer

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

Example

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.