Open In App

Scala List init() method with example

Last Updated : 29 Jul, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The init() method is utilized to find all the elements of the list except the last one.
Method Definition: def init: List[A] Return Type: It returns all the elements of the list except the last one.
Example #1: Scala
// Scala program of init()
// method

// Creating object
object GfG
{ 

    // Main method
    def main(args:Array[String])
    {
        // Creating a list
        val m1 = List(3, 6, 2, 9, 21)
        
        // Applying init method
        val result = m1.init
        
        // Displays output
        println(result)
    
    }
} 
Output:
List(3, 6, 2, 9)
Example #2: Scala
// Scala program of init()
// method

// Creating object
object GfG
{ 

    // Main method
    def main(args:Array[String])
    {
        // Creating a list
        val m1 = List(3)
        
        // Applying init method
        val result = m1.init
        
        // Displays output
        println(result)
    
    }
} 
Output:
List()

Next Article

Similar Reads