此方法返回此列表中指定元素出现的最后一个索引,如果此列表不包含该元素,则返回-1。
用法:
int lastIndexOf(Object o)
参数:该函数具有单个参数,即列表中要搜索的元素。
返回值:此方法返回列表中给定元素的最后出现索引,如果元素不在列表中,则返回“-1”。
下面的程序显示了此方法的实现。
示例1:
// Java code to show the implementation of
// lastIndexOf method in list interface
import java.util.*;
public class GfG {
// Driver code
public static void main(String[] args)
{
// Initializing a list of type Linkedlist
List l = new LinkedList<>();
l.add(1);
l.add(3);
l.add(5);
l.add(7);
l.add(3);
System.out.println(l);
System.out.println(l.lastIndexOf(3));
}
}
输出:
[1, 3, 5, 7, 3]
4
示例2:以下代码显示了使用Linkedlist实现list.lastIndexOf()的代码。
// Java code to show the implementation of
// lastIndexOf method in list interface
import java.util.*;
public class GfG {
// Driver code
public static void main(String[] args)
{
// Initializing a list of type Linkedlist
List l = new LinkedList<>();
l.add("10");
l.add("30");
l.add("50");
l.add("70");
l.add("30");
System.out.println(l);
System.out.println(l.lastIndexOf("30"));
}
}
输出:
[10, 30, 50, 70, 30]
4