Scala List lastIndexOf()方法及示例
lastIndexOf()方法用于查找本方法中作为参数出现的元素的最后出现的索引。
方法定义: def lastIndexOf(elem: A, end: Int):Int
返回类型。它返回本方法中作为参数的元素的最后出现的索引。
例子 #1:
// Scala program of lastIndexOf()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val m1 = List(3, 6, 2, 9, 21, 9)
// Applying lastIndexOf method
val result = m1.lastIndexOf(9)
// Displays output
println(result)
}
}
输出。
5
例子#2。
// Scala program of lastIndexOf()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val m1 = List(3, 6, 2, 9, 3, 21, 3)
// Applying lastIndexOf method
val result = m1.lastIndexOf(3)
// Displays output
println(result)
}
}
输出。
6