Open In App

Scala List indexOf() method with example

The indexOf() method is utilized to check the index of the element from the stated list present in the method as argument.

Method Definition: def indexOf(elem: A, from: Int): Int



Return Type: It returns the index of the element present in the argument.

Example #1:




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

Output:

3

Example #2:




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

Output:
0

Article Tags :