java.util.LinkedList.lastIndexOf(Object o)%uA0方法返回在此列表中指定元素的最后一个匹配项的索引,或者-1,如果此列表中不包含该元素。
声明
以下是java.util.LinkedList.lastIndexOf()方法的声明
public int lastIndexOf(Object o)
参数
-
o%uA0-- 要搜索的元素
返回值
这个方法返回在此列表中指定元素的最后一个匹配项的索引,或者-1,如果此列表中不包含该元素
异常
-
NA
例子
下面的示例演示java.util.LinkedList.lastIndexOf()方法的用法。
package com.yiibai 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