Home > Java > javaTutorial > body text

Java Example - Searching Linked List Elements

黄舟
Release: 2017-02-04 09:57:24
Original
1478 people have browsed it

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"));
   }}
Copy after login

The output of the above code is: :

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

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)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template