How to print a list in Scala

15 Apr, 2019
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 ListExample :// Scala program to create an empty list import scala.collection.immutable._  // Creating object object GFG{     // Main method     def main(args:Array[String])     {           // Creating an Empty List.         val emptylist: List[Nothing] = List()         println("The empty list is: " + emptylist)     } } Output:The empty list is: List() 
// Scala program to create an empty list import scala.collection.immutable._  // Creating object object GFG{     // Main method     def main(args:Array[String])     {           // Creating an Empty List.         val emptylist: List[Nothing] = List()         println("The empty list is: " + emptylist)     } } 
Output:
The empty list is: List()
 
Create a simple listExample :// Scala program to create a simple Immutable lists import scala.collection.immutable._  // Creating object object GFG {     // Main method     def main(args:Array[String])     {         // Creating and initializing immutable lists         val mylist: List[String] = List("Geeks", "For", "geeks")                 // Display the value of mylist1         println("List is: " + mylist)             } } Output:List is: List(Geeks, For, geeks) 
// Scala program to create a simple Immutable lists import scala.collection.immutable._  // Creating object object GFG {     // Main method     def main(args:Array[String])     {         // Creating and initializing immutable lists         val mylist: List[String] = List("Geeks", "For", "geeks")                 // Display the value of mylist1         println("List is: " + mylist)             } } 
Output:
List is: List(Geeks, For, geeks)
 
Using for loop to print elements of ListExample :// Scala program to print immutable lists import scala.collection.immutable._  // Creating object object GFG{     // Main method     def main(args:Array[String])     {         // Creating and initializing immutable lists         val mylist: List[String] = List("Geeks", "For",                                         "geeks", "is",                                        "a", "fabulous",                                         "portal")          // Display the value of mylist using for loop         for(element<-mylist)         {             println(element)         }     } } Output:Geeks
For
geeks
is
a
fabulous
portal
// Scala program to print immutable lists import scala.collection.immutable._  // Creating object object GFG{     // Main method     def main(args:Array[String])     {         // Creating and initializing immutable lists         val mylist: List[String] = List("Geeks", "For",                                         "geeks", "is",                                        "a", "fabulous",                                         "portal")          // Display the value of mylist using for loop         for(element<-mylist)         {             println(element)         }     } } 
Output:
Geeks
For
geeks
is
a
fabulous
portal
Scala
scala-collection
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Type Casting in Scala
Class and Object in Scala
Scala Tutorial – Learn Scala with Step By Step Guide
Scala String substring() method with example
Operators in Scala
Scala Constructors
Enumeration in Scala
Lambda Expression in Scala
How to Install Scala with VSCode?
Scala String replace() method with example