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 m 1 = List( 3 , 6 , 2 , 9 , 21 )
// Applying init method
val result = m 1 .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 m 1 = List( 3 )
// Applying init method
val result = m 1 .init
// Displays output
println(result)
}
} |
Output:
List()