Java 中的 AbstractSequentialList 的 indexOf() 方法
indexOf() 方法从 AbstractList 类继承而来。它用于返回该列表中指定元素的第一次出现的索引。如果列表为空,则返回 -1。
语法如下 −
public int indexOf(Object ele)
此处 ele 是你想要索引的元素。
若要在 Java 中使用 AbstractSequentialList 类,你需要导入以下软件包 −
import java.util.AbstractSequentialList;
以下是一个在 Java 中实现 AbstractSequentialList indexOf() 方法的示例 −
示例
import java.util.LinkedList; import java.util.AbstractSequentialList; public class Demo { public static void main(String[] args) { AbstractSequentialList<Integer> absSequential = new LinkedList<>(); absSequential.add(110); absSequential.add(320); absSequential.add(400); absSequential.add(550); absSequential.add(600); absSequential.add(700); absSequential.add(900); System.out.println("Elements in the AbstractSequentialList = "+absSequential); System.out.println("Elements in the AbstractSequentialList = "+absSequential.indexOf(550)); } }
输出
Elements in the AbstractSequentialList = [110, 320, 400, 550, 600, 700, 900] Elements in the AbstractSequentialList = 3
我们看另一个元素不存在的示例 −
示例
import java.util.LinkedList; import java.util.AbstractSequentialList; public class Demo { public static void main(String[] args) { AbstractSequentialList<Integer> absSequential = new LinkedList<>(); absSequential.add(110); absSequential.add(320); absSequential.add(400); absSequential.add(550); absSequential.add(600); absSequential.add(700); absSequential.add(900); System.out.println("Elements in the AbstractSequentialList = "+absSequential); System.out.println("Elements in the AbstractSequentialList = "+absSequential.indexOf(10)); } }
输出
Elements in the AbstractSequentialList = [110, 320, 400, 550, 600, 700, 900] Elements in the AbstractSequentialList = -1
广告