0 votes
in Scala Constructs by
Explain how you will append data to a list.

1 Answer

0 votes
by
A Scala list is a collection of data stored as a linked list. In Scala, a list is immutable, meaning it cannot be changed. To remedy this, Scala offers List-Buffer. There are multiple ways to update a list and add new elements such as using the ":+" method: It adds new elements at the end of a list.  

Syntax:

 list_Name :+ element_Name

Example:

 object Colours  

{

    def main(args: Array[String])  

{

        val myList = List ("Red" , "White")

        println("List’s Content : " + myList)         

        println(" Appending/adding elements at the end of list!")

        val updatedList = myList :+ "Blue"

        println("Updated List’s Content: " + updatedList)

    }

}

Output:  

List’s Content : List(Red, White)  

Appending/adding elements at the end of the list!

Updated List’s Content : List (Red, White, Blue)

Related questions

0 votes
asked Sep 11, 2022 in Scala Constructs by sharadyadav1986
0 votes
asked Aug 13, 2022 in Scala - The Diatonic Syallable by AdilsonLima
...