Java Examples - Linked List Element Search
The following example demonstrates using the linkedlistname.indexof(element) and linkedlistname.Lastindexof(elementname) methods to obtain the first and last occurrence positions of an element in a linked list:
Main.java file
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList lList = new LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
lList.add("2");
System.out.println("The first occurrence position of element 2:" + lList.indexOf("2"));
System.out.println("The last occurrence position of element 2:"+ lList.lastIndexOf("2"));
}
}
The output of running the above code is:
元素 2 第一次出现的位置:1 元素 2 最后一次出现的位置:5Other Extensions
Java Examples