Java 中 AbstractList 类的 indexOf() 方法
indexOf() 方法用于返回此列表中指定元素的首次出现处的索引。如果列表为空,则返回 -1。
语法如下。
int indexOf(Object ob)
其中,参数 ob 是要搜索的元素。
要操作 AbstractList 类,请导入以下包。
import java.util.AbstractList;
以下是 Java 中 AbstractlList 类的 indexOf() 方法的实现示例。
示例
import java.util.ArrayList;
import java.util.AbstractList;
public class Demo {
public static void main(String[] args) {
AbstractList<Integer> myList = new ArrayList<Integer>();
myList.add(5);
myList.add(20);
myList.add(35);
myList.add(47);
myList.add(55);
myList.add(70);
myList.add(100);
myList.add(140);
myList.add(100);
myList.add(250);
System.out.println("Elements in AbstractList = " + myList);
System.out.println("Last Index of the element 100 = " + myList.lastIndexOf(100));
}
}输出
Elements in AbstractList = [5, 20, 35, 47, 55, 70, 100, 140, 100, 250] Last Index of the element 100 = 8
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP