Skip to content
Related Articles
Open in App
Not now

Related Articles

Scala List indexOf() method with example

Improve Article
Save Article
Like Article
  • Last Updated : 29 Jul, 2019
Improve Article
Save Article
Like Article

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
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!