Open In App

Scala List lastIndexOf() method with example

The lastIndexOf() method is utilized to find the index of the last occurrence of the element present in this method as argument.

Method Definition: def lastIndexOf(elem: A, end: Int): Int



Return Type: It returns the index of the last occurrence of the element present in this method as argument.

Example #1:




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

Output:

5

Example #2:




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

Output:
6

Article Tags :