java.util.ArrayList.lastIndexOf(Object)%uA0方法返回指定元素的最后一个匹配项的索引在此列表中,或者-1,如果此列表中不包含该元素。
声明
以下是java.util.ArrayList.lastIndexOf()方法的声明
public int lastIndexOf(Object o)
参数
-
o%uA0-- 要搜索的元素。
返回值
此方法返回指定元素的最后一个匹配项的索引在此列表中,或者-1,如果此列表中不包含该元素。
异常
-
NA
例子
下面的示例演示java.util.ArrayList.lastIndexOf()方法的用法。
package com.yiibai import java.util.ArrayList public class ArrayListDemo { public static void main(String[] args) { // create an empty array list with an initial capacity ArrayList<String> arrlist = new ArrayList<String>(5) // use add() method to add values in the list arrlist.add("G") arrlist.add("E") arrlist.add("F") arrlist.add("M") arrlist.add("E") System.out.println("Size of list: " + arrlist.size()) // let us print all the values available in list for (String value : arrlist) { System.out.println("Value = " + value) } // Retrieving the index of last occurrence of element "E" int retval=arrlist.lastIndexOf("E") System.out.println("The last occurrence of E is at index " + retval) } }
让我们来编译和运行上面的程序,这将产生以下结果:
Size of list: 5 Value = G Value = E Value = F Value = M Value = E The last occurrence of E is at index 4