多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Java`ArrayList lastIndexOf(Object Obj)`方法 > 原文: [https://beginnersbook.com/2013/12/java-arraylist-lastindexofobject-0bj-method-example/](https://beginnersbook.com/2013/12/java-arraylist-lastindexofobject-0bj-method-example/) 方法`lastIndexOf(Object obj)`返回`ArrayList`中指定元素的最后一次出现的索引。如果列表中不存在指定的元素,则返回 -1。 `public int lastIndexOf(Object obj)` 这将返回`ArrayList`中元素`Obj`的最后一次出现的索引。 ### 例 在下面的示例中,我们有一个`Integer ArrayList`,它具有很少的重复元素。我们使用`lastIndexof` 方法获取少数元素的最后一个索引。 ```java package beginnersbook.com; import java.util.ArrayList; public class LastIndexOfExample { public static void main(String args[]) { //ArrayList of Integer Type ArrayList<Integer> al = new ArrayList<Integer>(); al.add(1); al.add(88); al.add(9); al.add(17); al.add(17); al.add(9); al.add(17); al.add(91); al.add(27); al.add(1); al.add(17); System.out.println("Last occurrence of element 1: "+al.lastIndexOf(1)); System.out.println("Last occurrence of element 9: "+al.lastIndexOf(9)); System.out.println("Last occurrence of element 17: "+al.lastIndexOf(17)); System.out.println("Last occurrence of element 91: "+al.lastIndexOf(91)); System.out.println("Last occurrence of element 88: "+al.lastIndexOf(88)); } } ``` 输出: ```java Last occurrence of element 1: 9 Last occurrence of element 9: 5 Last occurrence of element 17: 10 Last occurrence of element 91: 7 Last occurrence of element 88: 1 ```