Java Java.util.LinkedList.indexOf(), lastIndexof()方法
链接列表库还提供了 描述元素的第一个和最后一个索引 的功能,必须分别使用indexOf()和lastIndexOf()函数来查找。它们提供了多种多样的功能,因为在传统的链接列表中无法直接访问,因此对它的了解是有用的。
1. indexOf(Object o) : 这个方法返回指定元素在这个列表中 第一次出现的 索引 ,如果这个列表不包含该元素,则返回-1。
声明:
public int indexOf(Object o)
参数 :
o : 要搜索的元素
返回值 :
该方法返回指定元素在列表中第一次出现的索引。
该方法返回指定元素在这个列表中第一次出现的索引,或者如果这个列表
不包含该元素。
// 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 code to demonstrate the application
// of indexOf() in linked list
import java.util.*;
public class LinkedListIndexApp {
public static void main(String[] args)
{
// Declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements to check
list.add(1);
list.add(4);
list.add(3);
list.add(6);
list.add(7);
list.add(4);
list.add(8);
// printing the initial list
System.out.println("The initial Linked List is : " + list);
// computing result
int res = list.lastIndexOf(4) - list.indexOf(4) - 1;
// Printing the number of elements between 1st occurrence of 4 and last 4
// prints 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
极客教程