Open In App

Scala List init() method with example

Last Updated : 29 Jul, 2019
Improve
Improve
Like Article
Like
Save
Share
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 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 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()


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads