Home  >  Article  >  Java  >  Java Example - Searching Linked List Elements

Java Example - Searching Linked List Elements

黄舟
黄舟Original
2017-02-04 09:57:241471browse

The following example demonstrates using the linkedlistname.indexof(element) and linkedlistname.Lastindexof(elementname) methods to obtain the first and last occurrence positions of elements in the linked list:

/*
 author by w3cschool.cc
 Main.java
 */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("元素 2 第一次出现的位置:" + lList.indexOf("2"));
      System.out.println("元素 2 最后一次出现的位置:"+ lList.lastIndexOf("2"));
   }}

The output of the above code is: :

元素 2 第一次出现的位置:1
元素 2 最后一次出现的位置:5

The above is the Java example-linked list element search content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn