带有示例的 Scala List lastIndexOf() 方法
lastIndexOf()方法用于查找此方法中作为参数存在的元素的最后一次出现的索引。
Method Definition: def lastIndexOf(elem: A, end: Int): Int
Return Type: It returns the index of the last occurrence of the element present in this method as argument.
示例 #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