Home » Scala language

How to add elements to a List in Scala?

Scala | Adding elements to a List: In this tutorial, we are going to learn how to add elements to a List in Scala?
Submitted by Shivang Yadav, on April 16, 2020

In Scala, lists are immutable data structures in which adding new elements is not allowed. So, here we will solve this problem that is generally done in functional programming paradigms.

To add elements to a list there are two methods,

1) Appending elements to Scala list

As the list is immutable when adding a new element to it, we will append an element to a new list.

Done using the following operators,

  • Prepending operator (::)
  • Appending operator +:

Program:

object MyClass {
    def main(args: Array[String]) {
        var progLang = List("Java", "Scala")
        
        println("Programming languages: " + progLang)
        
        println("Prepending elements to list")
        progLang = "C++" :: progLang
        println("Programming languages: " + progLang)
        
        println("Appending elements to list")
        progLang = progLang :+ "Python"    
        println("Programming languages: " + progLang)
    }
}

Output

Programming languages: List(Java, Scala)
Prepending elements to list
Programming languages: List(C++, Java, Scala)
Appending elements to list
Programming languages: List(C++, Java, Scala, Python)

2) Using ListBuffer

ListBuffer is a mutable list that has characteristics of a List but elements can be added or removed from the list.

Program:

import scala.collection.mutable.ListBuffer

object MyClass {
    def main(args: Array[String]) {
        var progLang = ListBuffer("Java", "Scala")
        
        println("Programming Languages :" + progLang)
        
        println("Adding New elments to ListBuffer")
        progLang += "C++"
        
        println("Programming Languages :" + progLang)
    }
}

Output

Programming Languages :ListBuffer(Java, Scala)
Adding New elments to ListBuffer
Programming Languages :ListBuffer(Java, Scala, C++)

You can convert this ListBuffer to a List if you need to use some characteristics of list:

    val listName = listBufferName.toList

Creating empty ListBiffer in Scala

You can also create an empty ListBuffer, just by mentioning its datatype.

Syntax:

    var listBufferName = new ListBuffer[dataType]()

Program:

import scala.collection.mutable.ListBuffer

object MyClass {
    def main(args: Array[String]) {
        var progLang = ListBuffer[String]()
        println("Programming Languages :" + progLang)
        println("Adding New elments to ListBuffer")
        progLang += "C++"
        progLang += "Java"
        progLang += "Scala"
        println("Programming Languages :" + progLang)
    }
}

Output

Programming Languages :ListBuffer()
Adding New elments to ListBuffer
Programming Languages :ListBuffer(C++, Java, Scala)





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.