Q:

Write a Scala program to find the index of an element in a given list

0

Write a Scala program to find the index of an element in a given list.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

object Scala_List
{
  def main(args: Array[String]): Unit = 
 {
   val colors = List("Red","Blue","Black","Green","White")
   println("Original lists:")
   println(colors)  
   println("Index of 'Red':", colors.indexOf("Red"))
   println("Index of 'Blue':", colors.indexOf("Blue"))
   println("Index of 'Black':", colors.indexOf("Black"))
   println("Index of 'Green':", colors.indexOf("Green"))
   println("Index of 'White':", colors.indexOf("White"))   
  }
}

Sample Output:

Original lists:
List(Red, Blue, Black, Green, White)
(Index of 'Red':,0)
(Index of 'Blue':,1)
(Index of 'Black':,2)
(Index of 'Green':,3)
(Index of 'White':,4)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now