Home  >  Article  >  Java  >  Use the removeLast() method of the LinkedList class to delete the last element in the linked list

Use the removeLast() method of the LinkedList class to delete the last element in the linked list

PHPz
PHPzOriginal
2023-07-24 17:13:462593browse

Use the removeLast() method of the LinkedList class to delete the last element in the linked list

LinkedList is a common data structure in the Java collection framework. It stores elements in the form of a doubly linked list. Through the methods provided by the LinkedList class, we can easily operate on the linked list, such as adding, deleting and modifying elements.

In some scenarios, we may need to delete the last element in the linked list. The LinkedList class provides the removeLast() method to implement this function. This article will introduce through code examples how to use the removeLast() method to delete the last element in the linked list.

First, we need to create a LinkedList object and add some elements to it for deletion.

import java.util.LinkedList;

public class RemoveLastExample {
    public static void main(String[] args) {
        // 创建一个LinkedList对象
        LinkedList<String> linkedList = new LinkedList<>();

        // 向链表中添加元素
        linkedList.add("元素1");
        linkedList.add("元素2");
        linkedList.add("元素3");
        linkedList.add("元素4");

        // 输出链表中的元素
        System.out.println("链表中的元素:" + linkedList);

        // 调用removeLast()方法删除链表中的最后一个元素
        String removedElement = linkedList.removeLast();

        // 输出删除的元素
        System.out.println("被删除的元素:" + removedElement);

        // 输出删除后的链表中的元素
        System.out.println("删除后的链表中的元素:" + linkedList);
    }
}

The above code first imports the LinkedList class and creates a LinkedList object. Then, four elements are added to the linked list through the add() method. Next, the last element in the linked list is deleted through the removeLast() method, and the deleted element is assigned to the removedElement variable. Finally, the elements in the deleted linked list and the deleted elements are printed through the output statement.

Run the above code, we can see the output as follows:

链表中的元素:[元素1, 元素2, 元素3, 元素4]
被删除的元素:元素4
删除后的链表中的元素:[元素1, 元素2, 元素3]

You can see that the last element "Element 4" has been successfully deleted. By calling the removeLast() method of the LinkedList class, we can easily delete the last element in the linked list.

It should be noted that if the linked list is empty (that is, there are no elements), then calling the removeLast() method will throw a NoSuchElementException exception. In actual use, we should check whether the linked list is empty before calling the removeLast() method.

To sum up, this article introduces how to use the removeLast() method of the LinkedList class to delete the last element in the linked list, and gives corresponding code examples. By rationally utilizing the methods provided by the LinkedList class, we can operate elements in the linked list more flexibly.

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

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