Skip to content
Related Articles

Related Articles

Improve Article
Scala List indexOf() method with example
  • Last Updated : 29 Jul, 2019

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :