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 :



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    The empty list is: List()

     

  • Create a simple list
    Example :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    List is: List(Geeks, For, geeks)

     

  • Using for loop to print elements of List
    Example :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Geeks
    For
    geeks
    is
    a
    fabulous
    portal


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.