该indexOf()
方法从AbstractList类继承。它用于返回此列表中指定元素的首次出现的索引。如果列表为空,则返回-1。
语法如下-
public int indexOf(Object ele)
在这里,ele是您想要为其索引的元素。
要使用Java中的AbstractSequentialList类,您需要导入以下包-
import java.util.AbstractSequentialList;
以下是indexOf()
在Java中实现AbstractSequentialList方法的示例-
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