Scala Programming: Find the index of an element in a given list
Scala Programming List Exercise-11 with Solution
Write a Scala program to find the index of an element in a given list.
Sample Solution:
Scala Code:
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)
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Scala program to find the first and last element of given list.
Next:Write a Scala program to merge (concatenate) given lists.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/scala-exercises/list/scala-list-exercise-11.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics