带有示例的 Scala List indexOf() 方法
indexOf()方法用于从作为参数的方法中存在的所述列表中检查元素的索引。
Method Definition: def indexOf(elem: A, from: Int): Int
Return Type: It returns the index of the element present in the argument.
示例 #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)
}
}
输出:
3
示例 #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)
}
}
输出:
0