📜  Java中的 ArrayList 和 LinkedList remove() 方法及示例(1)

📅  最后修改于: 2023-12-03 15:01:50.561000             🧑  作者: Mango

Introduction to the remove() method in ArrayList and LinkedList in Java

The ArrayList and LinkedList are two commonly used classes in Java for implementing dynamic arrays and linked lists, respectively. Both classes provide a remove() method that allows removing elements from the list. In this article, we will explore the remove() method in both classes and provide some examples to illustrate its usage.

ArrayList remove() method

The ArrayList class in Java provides the remove() method that allows removing elements from the list based on index or object value. Here is the syntax of the remove() method in ArrayList:

public E remove(int index)

This method takes the index of the element to be removed as a parameter and returns the element that was removed. The size of the ArrayList is automatically decremented by 1 after removing the element. If the index is out of bounds (i.e., less than 0 or greater than or equal to the size of the list), an IndexOutOfBoundsException is thrown.

Example 1: Removing element by index in ArrayList

import java.util.ArrayList;

public class ArrayListExample {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Mango");
        
        System.out.println("Before removal: " + fruits);
        
        String removedElement = fruits.remove(1);
        System.out.println("Removed element: " + removedElement);
        
        System.out.println("After removal: " + fruits);
    }
}

Output:

Before removal: [Apple, Banana, Mango]
Removed element: Banana
After removal: [Apple, Mango]
LinkedList remove() method

The LinkedList class in Java provides multiple remove() methods that allow removing elements from the list based on different conditions. Here are the syntaxes of the remove() methods in LinkedList:

Removing element by index:

public E remove(int index)

This method takes the index of the element to be removed as a parameter and returns the element that was removed. The size of the LinkedList is automatically decremented by 1 after removing the element.

Removing element by object value:

public boolean remove(Object o)

This method takes the object value that needs to be removed as a parameter. It returns true if the element is successfully removed, otherwise false.

Example 2: Removing elements by index and object value in LinkedList

import java.util.LinkedList;

public class LinkedListExample {
    public static void main(String[] args) {
        LinkedList<String> colors = new LinkedList<>();
        colors.add("Red");
        colors.add("Blue");
        colors.add("Green");
        
        System.out.println("Before removal: " + colors);
        
        String removedElement = colors.remove(1);
        System.out.println("Removed element by index: " + removedElement);
        
        boolean isRemoved = colors.remove("Red");
        System.out.println("Element 'Red' removed: " + isRemoved);
        
        System.out.println("After removal: " + colors);
    }
}

Output:

Before removal: [Red, Blue, Green]
Removed element by index: Blue
Element 'Red' removed: true
After removal: [Green]
Conclusion

In this article, we discussed the remove() method available in ArrayList and LinkedList classes in Java. We explored the syntax and usage of the remove() method with relevant examples. The remove() method is a useful operation that allows programmers to remove elements from the dynamic arrays (ArrayList) and linked lists (LinkedList) in Java.