🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Java `ArrayList indexOf()`方法 > 原文: [https://beginnersbook.com/2013/12/java-arraylist-indexof-method-example/](https://beginnersbook.com/2013/12/java-arraylist-indexof-method-example/) `ArrayList`类方法`indexOf(Object o)`用于查找列表中特定元素的索引。 `public int indexOf(Object o)` 如果列表中不存在指定的元素,则此方法返回 -1。 ### 例 ```java package beginnersbook.com; import java.util.ArrayList; public class IndexOfExample { public static void main(String[] args) { ArrayList<String> al = new ArrayList<String>(); al.add("AB"); al.add("CD"); al.add("EF"); al.add("GH"); al.add("IJ"); al.add("KL"); al.add("MN"); System.out.println("Index of 'AB': "+al.indexOf("AB")); System.out.println("Index of 'KL': "+al.indexOf("KL")); System.out.println("Index of 'AA': "+al.indexOf("AA")); System.out.println("Index of 'EF': "+al.indexOf("EF")); } } ``` 输出: ```java Index of 'AB': 0 Index of 'KL': 5 Index of 'AA': -1 Index of 'EF': 2 ``` #### 参考: [`ArrayList.indexOf(Object)`](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#indexOf(java.lang.Object))