Java Java.util.LinkedList.indexOf(), lastIndexof()方法及示例

Java Java.util.LinkedList.indexOf(), lastIndexof()方法及示例

链表库还提供了使用indexOf()和lastIndexOf()函数分别查找元素的第一个和最后一个索引的功能。 它们提供了多种选择,因为在传统的链表中没有直接访问的方式,因此了解它是有用的。

1. indexOf(Object o) : 此方法返回此列表中指定元素的 第一次出现的索引 ,如果此列表不包含元素,则返回-1。

声明 :
public int indexOf(Object o)
参数 :
o : 要搜索的元素
返回值 :
此方法返回此列表中指定元素的 第一次出现的索引 ,如果此列表不包含元素,则返回-1。

// Java code to demonstrate the working
// of indexOf(Object o) in linked list
import java.util.*;
public class LinkedListIndexOf {
public static void main(String[] args)
{
// Declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements
list.add("Geeks");
list.add(4);
list.add("Geeks");
list.add(8);
// printing the initial list
System.out.println("The initial Linked List is : " + list);
// Retrieving index of 1st occurrence of "Geeks"
// Prints 0
System.out.println("Index of 1st occurrence of Geeks : "
+ list.indexOf("Geeks"));
// Retrieving index of 1st occurrence of "Astha"
// Prints -1
// element not present
System.out.println("Index of 1st occurrence of Astha : "
+ list.indexOf("Astha"));
}
}

输出 :

The initial Linked List is : [Geeks, 4, Geeks, 8]
Index of 1st occurrence of Geeks : 0
Index of 1st occurrence of Astha : -1

2. lastIndexOf(Object o) : 此方法返回此列表中指定元素的 最后一个索引 ,如果此列表不包含元素,则返回-1。

声明 :
public int lastIndexOf(Object o)
参数 :
o : 要搜索的元素
返回值 :
此方法返回此列表中指定元素的 最后一个索引 ,如果此列表不包含元素,则返回-1。

// Java code to demonstrate the working
// of lastIndexOf(Object o) in linked list
import java.util.*;
public class LinkedListLastindexOf {
public static void main(String[] args)
{
// Declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements
list.add("Geeks");
list.add(4);
list.add("Geeks");
list.add(8);
// printing the initial list
System.out.println("The initial Linked List is : " + list);
// Retrieving index of last occurrence of "Geeks"
// Prints 2
System.out.println("Index of last occurrence of Geeks : " 
+ list.lastIndexOf("Geeks"));
// Retrieving index of last occurrence of "Astha"
// Prints -1
// element not present
System.out.println("Index of last occurrence of Astha : " 
+ list.lastIndexOf("Astha"));
}
}

输出 :

The initial Linked List is : [Geeks, 4, Geeks, 8]
Index of last occurrence of Geeks : 2
Index of last occurrence of Astha : -1

实际应用: 由于这两个函数都显示特定数字或字符串等的第一个和最后一个索引,因此它们可用于计算在这些值的最后和第一个出现之间正在到来的元素、人数等。下面给出一个小例子。

//Java代码示例,演示indexOf()在链表中的应用
import java.util.*;
public class LinkedListIndexApp {
   
public static void main(String[] args)
    {
   
        //声明一个LinkedList
        LinkedList list = new LinkedList();
   
        //添加要检查的元素
        list.add(1);
        list.add(4);
        list.add(3);
        list.add(6);
        list.add(7);
        list.add(4);
        list.add(8);
   
        //打印初始列表
        System.out.println("The initial Linked List is : " + list);
   
        //计算结果
        int res = list.lastIndexOf(4) - list.indexOf(4) - 1;
   
        //打印第一个4和最后一个4之间的元素数量
        //打印3
        System.out.println("The no. between 4s are :  " + res);
    }
}

输出:

The initial Linked List is : [1, 4, 3, 6, 7, 4, 8]
The no. between 4s are :  3

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程