Open In App

Scala List init() method with example

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


Last Updated : 29 Jul, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads