Java中AbstractSequentialList的indexOf()方法
该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
热门推荐
10 医院探望朋友祝福语简短
11 毕业祝福语简短女朋友
12 春节虎年爸妈祝福语简短
13 儿童毕业祝福语 简短6
14 开工仪式横幅祝福语简短
15 孩子日常祝福语大全简短
16 清晨寄语诗句祝福语简短
17 结婚送亲认亲祝福语简短
18 虎年喝酒拜年祝福语简短