Java.util.LinkedList.indexOf(), Java中的 lastIndexof()
链表库还提供描述必须分别使用 indexOf() 和 lastIndexOf() 函数找到的元素的第一个和最后一个索引。它们提供了多种选择,因为直接访问在传统制作的链表中不可用,因此了解它是有用的。
1. indexOf(Object o) :此方法返回此列表中指定元素第一次出现的索引,如果此列表不包含该元素,则返回-1。
Declaration :
public int indexOf(Object o)
Parameters :
o : element to search for
Return Value :
This method returns the index of the first occurrence of
the specified element in this list, or -1 if this list
does not contain the element.
// 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。
Declaration :
public int lastIndexOf(Object o)
Parameters :
o : element to search for
Return Value :
This method returns the index of the last occurrence
of the specified element in this list, or -1 if this
list does not contain the element
// 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