Java.util.LinkedList.lastIndexOf() 方法
描述
java.util.LinkedList.lastIndexOf(Object o) 方法返回此列表中指定元素的最后一次出现的索引,如果此列表不包含该元素,则返回 -1。
声明
以下是 java.util.LinkedList.lastIndexOf() 方法的声明
public int lastIndexOf(Object o)
参数
o − 要搜索的元素
返回值
此方法返回此列表中指定元素最后一次出现的索引,如果此列表不包含该元素,则返回 -1
异常
NA
示例
下面的例子展示了 java.util.LinkedList.lastIndexOf() 方法的使用。
package com.tutorialspoint;
import java.util.*;
public class LinkedListDemo {
public static void main(String[] args) {
// create a LinkedList
LinkedList list = new LinkedList();
// add some elements
list.add("Hello");
list.add(2);
list.add("Chocolate");
list.add("10");
list.add("Hello");
// print the list
System.out.println("LinkedList:" + list);
// get the last index for "Hello"
System.out.println("Index for Hello:" + list.lastIndexOf("Hello"));
// get the index for "Coffee"
System.out.println("Index for Coffee:" + list.indexOf("Coffee"));
}
}
让我们编译并运行上面的程序,这将产生以下结果 −
LinkedList:[Hello, 2, Chocolate, 10, Hello] Index for Hello:4 Index for Coffee:-1

