How to print a list in Scala
In Scala, list is defined under scala.collection.immutable package. A list is a collection of same type elements which contains immutable data.
There are multiple ways to create a List in Scala. Let’s see few basic on how to create a Scala List.
- Create an empty List
Example :// Scala program to create an empty listimportscala.collection.immutable._// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating an Empty List.valemptylist:List[Nothing]=List()println("The empty list is: "+ emptylist)}}chevron_rightfilter_noneOutput:
The empty list is: List()
- Create a simple list
Example :// Scala program to create a simple Immutable listsimportscala.collection.immutable._// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating and initializing immutable listsvalmylist:List[String]=List("Geeks","For","geeks")// Display the value of mylist1println("List is: "+ mylist)}}chevron_rightfilter_noneOutput:
List is: List(Geeks, For, geeks)
- Using for loop to print elements of List
Example :// Scala program to print immutable listsimportscala.collection.immutable._// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating and initializing immutable listsvalmylist:List[String]=List("Geeks","For","geeks","is","a","fabulous","portal")// Display the value of mylist using for loopfor(element<-mylist){println(element)}}}chevron_rightfilter_noneOutput:
Geeks For geeks is a fabulous portal
Recommended Posts:
- Scala List concatenation with example
- How to get the first element of List in Scala
- Scala List min() method with example
- Scala List max() method with example
- Scala List contains() method with example
- Scala List sum() method with example
- How to reverse a list in scala
- Scala List take() method with example
- Scala List ::() operator with example
- Scala List :::() operator with example
- Scala List +() operator with example
- Scala List map() method with example
- Scala List init() method with example
- Scala List forall() method with example
- Scala List length() method with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



