Home > Java > javaTutorial > Interpretation of Java documentation: Analysis of the indexOf() method function of the LinkedList class

Interpretation of Java documentation: Analysis of the indexOf() method function of the LinkedList class

WBOY
Release: 2023-11-03 12:26:11
Original
1696 people have browsed it

Interpretation of Java documentation: Analysis of the indexOf() method function of the LinkedList class

LinkedList is a doubly linked list implementation class in the Java collection framework. It can dynamically add or delete elements and support insertion and deletion operations at any location. In the LinkedList class, the indexOf() method is a commonly used operation. It is used to find the position where a specified element first appears in the linked list. This article will introduce in detail the function of this method and how to use it.

The syntax of the indexOf() method of the LinkedList class is as follows:

public int indexOf(Object o) {
    int index = 0;
    if (o == null) {
        for (Node<E> x = first; x != null; x = x.next) {
            if (x.item == null) {
                return index;
            }
            index++;
        }
    } else {
        for (Node<E> x = first; x != null; x = x.next) {
            if (o.equals(x.item)) {
                return index;
            }
            index++;
        }
    }
    return -1;
}
Copy after login

This method receives an Object type parameter o as input, representing the element to be found. The method returns a value of type int, indicating the position where the element first appears in the linked list (starting from 0). If the element does not exist in the linked list, it returns -1.

In order to better understand the implementation principle of this method, a detailed functional analysis of this method will be carried out below.

  1. Traverse the linked list

First of all, in order to find the position of the element in the linked list, we need to traverse the entire linked list and search one by one starting from the first node of the linked list, so the The method uses two for loops. Their functions are to traverse the linked list and find elements respectively. The code is as follows:

for (Node<E> x = first; x != null; x = x.next) {
    if (o.equals(x.item)) {
        return index;
    }
    index++;
}
Copy after login

This code means starting from the first node of the linked list, first, and traversing the nodes of the linked list one by one. , until the end of the linked list (that is, when x is null). During each traversal, we determine whether the element in the current node is equal to the element to be found. If equal, return the position of the node in the linked list (ie, the index variable), otherwise continue with the next traversal.

  1. Determine whether the element exists

In the process of traversing nodes, we need to determine whether the specified element exists in the linked list. An if statement is used in the method to implement this function. The code is as follows:

if (o.equals(x.item)) {
    return index;
}
Copy after login

In this statement, o.equals(x.item) indicates whether the element represented by o is equal to the element in the node currently looped to. x.item, if equal, return the position of the node in the linked list (i.e. index variable), otherwise continue with the next traversal.

It should be noted that when determining whether an element exists, if the element is null, item==null should be used instead of o.equals(x.item). Therefore, this method also needs an additional if statement to handle this situation. The code is as follows:

if (x.item == null) {
    return index;
}
Copy after login

x.item==null in this statement means that the element in the node currently looped to is null. If you want to find The element is also null, then the position of the node in the linked list (i.e. the index variable) is returned, otherwise the next traversal continues.

  1. Return element position

Finally, when the element you are looking for is still not found after traversing the entire linked list, the method will return -1, indicating that the element does not exist in the linked list middle. The code is as follows:

return -1;
Copy after login

Therefore, when using this method, we should judge whether the element we are looking for exists in the linked list based on the return value. If the return value is -1, it means that the element does not exist, otherwise The number returned is the position of the first occurrence of the element in the linked list.

The following is a specific code example to illustrate the use of this method:

import java.util.LinkedList;

public class LinkedListDemo {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<String>();
        list.add("apple");
        list.add("banana");
        list.add("grape");
        list.add("orange");
        list.add("peach");

        int index = list.indexOf("apple");
        if (index != -1) {
            System.out.println("apple在链表中的位置是:" + index);
        } else {
            System.out.println("链表中不存在apple");
        }

        index = list.indexOf("watermelon");
        if (index != -1) {
            System.out.println("watermelon在链表中的位置是:" + index);
        } else {
            System.out.println("链表中不存在watermelon");
        }
    }
}
Copy after login

This code first creates a LinkedList object list and adds 5 elements. Then, it uses the indexOf() method to find the positions of the two elements "apple" and "watermelon" in the linked list, and determines whether the two elements exist in the linked list based on the return value, and finally outputs the result.

When we run this code, we can get the following output:

apple在链表中的位置是:0
链表中不存在watermelon
Copy after login

It can be seen that this method is very powerful and can help us quickly find the position of the specified element in the linked list and realize our own business logic. However, when using this method, please note that its time complexity is O(n), that is, as the number of linked list elements increases, the search time will become longer and longer, so when using this method, you should try to avoid traversing the entire Linked list, but should be optimized according to the actual situation.

The above is the detailed content of Interpretation of Java documentation: Analysis of the indexOf() method function of the LinkedList class. For more information, please follow other related articles on the PHP Chinese website!

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