Home > Java > javaTutorial > Use the indexOf() method of the LinkedList class to get the index of an element in the linked list

Use the indexOf() method of the LinkedList class to get the index of an element in the linked list

WBOY
Release: 2023-07-24 18:52:55
Original
1396 people have browsed it

Use the indexOf() method of the LinkedList class to obtain the index of the element in the linked list

LinkedList is one of the commonly used linked list implementation classes in Java. It provides a series of convenient methods for adding, deleting, and traversing elements in the linked list. Among them, the indexOf(Object o) method can be used to obtain the index of the specified element in the linked list.

In the LinkedList class, the length of the linked list is not fixed, and elements can be dynamically added or deleted as needed. This is different from an array, which has a fixed length. As elements are added or deleted, the array may need to be expanded or reduced frequently. Therefore, the LinkedList class has obvious advantages in certain scenarios.

Using the indexOf(Object o) method can easily obtain the index of the specified element in the linked list. The prototype of this method is:

int indexOf(Object o)

Its return value is the index of the first occurrence of the specified element in the linked list, if the element does not exist in the linked list , then -1 is returned.

The following is a sample code to illustrate how to use the indexOf() method of LinkedList:

import java.util.LinkedList;

public class LinkedListExample {

   public static void main(String[] args) {
   
      LinkedList<String> linkedList = new LinkedList<>();
      
      // 添加元素
      linkedList.add("元素1");
      linkedList.add("元素2");
      linkedList.add("元素3");
      linkedList.add("元素4");
      linkedList.add("元素5");
      
      // 获取元素的索引
      int index = linkedList.indexOf("元素3");
      System.out.println("元素3的索引为:" + index);
      
      // 查找不存在的元素
      int notFoundIndex = linkedList.indexOf("元素6");
      System.out.println("元素6的索引为:" + notFoundIndex);
   }
}
Copy after login

Run the above code, the output result is:

The index of element 3 is: 2
The index of element 6 is: -1

As can be seen from the output result, the index of element "element 3" in the linked list is 2, while the element "element 6" does not exist in the linked list. So the return value is -1.

Use the indexOf() method of the LinkedList class to easily obtain the index of the specified element in the linked list. Its time complexity is O(n), where n is the length of the linked list. In practical applications, we can determine whether the element exists in the linked list based on whether the return value is -1, and perform corresponding processing.

In short, LinkedList is a good choice for scenarios that require frequent addition and deletion of elements. The indexOf() method can be used to easily obtain the index of the specified element in the linked list, so as to locate and operate the elements in the linked list.

The above is the detailed content of Use the indexOf() method of the LinkedList class to get the index of an element in the linked list. For more information, please follow other related articles on the PHP Chinese website!

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