×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Scala ListBuffer – Creating Mutable List

By IncludeHelp Last updated : October 20, 2024

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.

Import statement

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

import scala.collection.mutable.ListBuffer

Creating an empty ListBuffer

Syntax to create an empty ListBuffer in Scala:

var ListBuffer_name = new ListBuffer[datatype]()

Creating a ListBuffer with elements

Syntax to create a ListBuffer with elements in Scala:

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

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.

Syntax

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

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)

Using append() Method

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

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)

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)

Using remove() Method

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

//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!

Load comments ↻





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