Scala ListBuffer – Creating Mutable List

Scala ListBuffer: ListBuffer is a mutable list which used to create alterable lists. Here, we will see different operations on Scala listBuffer with examples.
Submitted by Shivang Yadav, on May 13, 2020

Scala Lists

A List is a collection of immutable elements of the same data type. In scala, the list represents a linked-list.

You cannot update a list as it is immutable, so for functions like add, update, delete we need to create a mutable list which is done using ListBuffer.

Scala ListBuffer

ListBuffer is a special type of list which is immutable i.e. updating ListBuffer is possible.

Syntax to create an empty ListBuffer in Scala,

    var ListBuffer_name = new ListBuffer[datatype]()

Syntax to create a ListBuffer with elements in Scala,

    var ListBuffer_name = new ListBuffer(element1, element2, element3...)

For creating ListBuffer in Scala, we need to import the ListBuffer class to our program using the following statement,

    import scala.collection.mutable.ListBuffer

ListBuffer operations

We can perform update operations like adding and deleting elements in a ListBuffer.

1) Accessing elements of a ListBuffer

We can access any element of the ListBuffer in the same way that we used to access elements in list using the index value of the element in the ListBuffer.

The syntax for accessing ith element of the ListBuffer,

    listBuffer(i)

Program to access elements of a ListBuffer

import scala.collection.mutable.ListBuffer

object MyObject {
    def main(args: Array[String]) {
        var city = ListBuffer("Delhi" , "Mumbai" , "Indore" , "Pune")
        println("Acessing 2nd element of the ListBuffer : " + city(1))
        println("Acessing 3nd element of the ListBuffer : " + city(2))
    }
}

Output:

Acessing 2nd element of the ListBuffer : Mumbai
Acessing 3nd element of the ListBuffer : Indore

2) Adding elements to a ListBuffer

Adding of new elements in the same ListBuffer is possible. There are several methods that can be used to add elements to ListBuffer. They are,

  1. Using += operator
  2. Using append() method

Syntax:

    //Syntax to add a single element to ListBuffer,
    ListBuffer += element
    //Syntax to add multiple elements to ListBuffer,
    ListBuffer += (element1, element2, element3, ...)

Note: Both the above syntaxes are used to append() the elements to the ListBuffer, i.e. elements will be added at the end of the ListBuffer. But you can also prepend list in Scala i.e. add elements at the start of the ListBuffer using the following syntax,

    element +=: ListBuffer

Program for adding elements to ListBuffer using += operator

import scala.collection.mutable.ListBuffer

object MyObject {
    def main(args: Array[String]) {
        var bikes = ListBuffer[String]()
        println("Empty ListBuffer : " + bikes)
        
        println("Adding a single element to the ListBuffer")
        bikes += "ThunderBird 350"
        println("ListBuffer : " + bikes)
        
        println("Adding multiple element to the listBuffer")
        bikes += ( "Iron 833", "S1000RR" )
        println("ListBuffer : " + bikes)
        
        println("Adding elements at the start of the ListBuffer")
        ("Pulsar 150") +=: bikes
        println("ListBuffer : " + bikes)
    }
}

Output:

Empty ListBuffer : ListBuffer()
Adding a single element to the ListBuffer
ListBuffer : ListBuffer(ThunderBird 350)
Adding multiple element to the listBuffer
ListBuffer : ListBuffer(ThunderBird 350, Iron 833, S1000RR)
Adding elements at the start of the ListBuffer
ListBuffer : ListBuffer(Pulsar 150, ThunderBird 350, Iron 833, S1000RR)

The append() method is used to add elements at the end of the ListBuffer.

Syntax:

    ListBuffer.append(element1, element2, element3...)

Program for adding elements to ListBuffer using the append() method

import scala.collection.mutable.ListBuffer

object MyObject {
    def main(args: Array[String]) {
        var lang = ListBuffer[String]()
        println("Empty ListBuffer : " + lang)

        println("Adding a single element to the ListBuffer")
        lang.append("C")
        println("ListBuffer : " + lang)

        println("Adding multiple element to the listBuffer")
        lang.append( "C++", "Scala" , "Python" )
        println("ListBuffer : " + lang)
    }
}

Output:

Empty ListBuffer : ListBuffer()
Adding a single element to the ListBuffer
ListBuffer : ListBuffer(C)
Adding multiple element to the listBuffer
ListBuffer : ListBuffer(C, C++, Scala, Python)

3) Deleting elements from a ListBuffer

In ListBuffer, you can delete elements. The are several methods to delete elements from a ListBuffer. They are

  1. Using -= operator
  2. Using remove() method

We can delete single or multiple elements using the -= operator.

Syntax:

    //Deleting a single element from ListBuffer,
    ListBuffer -= element
    //Deleting multiple elements from ListBuffer,
    ListBuffer -= (element1, element2, element3, ...)

Program to delete elements from a ListBuffer using -= method

import scala.collection.mutable.ListBuffer

object MyObject {
    def main(args: Array[String]) {
        var listbuffer = ListBuffer(4, 5, 1, 7 ,21, 43, 99)
        println("ListBuffer : " + listbuffer)

        println("Removing single element to the ListBuffer")
        listbuffer -= 43
        println("ListBuffer : " + listbuffer)

        println("Removing multiple element to the ListBuffer")
        listbuffer -= (4, 99, 1)
        println("ListBuffer : " + listbuffer)
    }
}

Output:

ListBuffer : ListBuffer(4, 5, 1, 7, 21, 43, 99)
Removing single element to the ListBuffer
ListBuffer : ListBuffer(4, 5, 1, 7, 21, 99)
Removing multiple element to the ListBuffer
ListBuffer : ListBuffer(5, 7, 21)

The remove() method can delete single and multiple elements from the ListBuffer. The element is deleted using its index.

Syntax:

    //Syntax for deleting a single element from ListBuffer, 
    ListBuffer.remove(index)
    //Syntax for deleting multiple elements from ListBuffer, 
    ListBuffer.remove(startindex , endindex)

Program to delete elements from ListBuffer using remove() method

import scala.collection.mutable.ListBuffer

object MyObject {
    def main(args: Array[String]) {
        var listbuffer = ListBuffer(4, 5, 1, 7 ,21, 43, 99)
        println("ListBuffer : " + listbuffer)
        
        println("Removing single element to the ListBuffer")
        listbuffer.remove(4)
        println("ListBuffer : " + listbuffer)
        
        println("Removing multiple element to the ListBuffer")
        listbuffer.remove(1, 4)
        println("ListBuffer : " + listbuffer)
    }
}

Output:

ListBuffer : ListBuffer(4, 5, 1, 7, 21, 43, 99)
Removing single element to the ListBuffer
ListBuffer : ListBuffer(4, 5, 1, 7, 43, 99)
Removing multiple element to the ListBuffer
ListBuffer : ListBuffer(4, 99)





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.