Skip to content
Related Articles

Related Articles

Scala List init() method with example
  • Last Updated : 29 Jul, 2019

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :