Home  >  Article  >  Java  >  Java uses the remove() function of the ArrayList class to delete elements from the collection

Java uses the remove() function of the ArrayList class to delete elements from the collection

王林
王林Original
2023-07-24 21:17:051806browse

Java uses the remove() function of the ArrayList class to delete elements in the collection

ArrayList is one of the commonly used collection classes in Java. It provides an implementation of a dynamic array. ArrayList can store different types of elements and can be added and deleted at any time as needed.

In ArrayList, we often need to delete specific elements in the collection. Java provides the remove() function to implement this function. Next, we will introduce in detail how to use the remove() function of ArrayList to delete elements in the collection.

The remove() function of ArrayList has two signatures:

  1. boolean remove(Object o): Remove the specified element from the ArrayList.
  2. E remove(int index): Delete elements in ArrayList based on index position.

The following is a detailed explanation and sample code for these two functions.

  1. Use the remove(Object o) function to delete the specified element:

The remove(Object o) function is used to delete the specified element from the ArrayList. It iterates through the ArrayList, finds the first element equal to the specified element, and removes it from the ArrayList. If the deletion is successful, return true; otherwise, return false.

The sample code is as follows:

import java.util.ArrayList;

public class ArrayListDemo {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("apple");
        list.add("banana");
        list.add("orange");
        
        System.out.println("删除之前的ArrayList:" + list);
        
        boolean result = list.remove("banana");
        
        if(result){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
        
        System.out.println("删除之后的ArrayList:" + list);
    }
}

The running results are as follows:

删除之前的ArrayList:[apple, banana, orange]
删除成功
删除之后的ArrayList:[apple, orange]

As can be seen from the running results, we used the remove() function to successfully delete the element "banana" in the ArrayList ".

  1. Use the remove(int index) function to delete elements based on the index position:

The remove(int index) function is used to delete elements in the ArrayList based on the index position. It will remove the element at the specified index position from the ArrayList and move subsequent elements forward.

The sample code is as follows:

import java.util.ArrayList;

public class ArrayListDemo {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("apple");
        list.add("banana");
        list.add("orange");
        
        System.out.println("删除之前的ArrayList:" + list);
        
        String removedElement = list.remove(1);
        
        System.out.println("被删除的元素:" + removedElement);
        System.out.println("删除之后的ArrayList:" + list);
    }
}

The running results are as follows:

删除之前的ArrayList:[apple, banana, orange]
被删除的元素:banana
删除之后的ArrayList:[apple, orange]

As can be seen from the running results, we use the remove() function to successfully delete the items in the ArrayList based on the index position. Element "banana".

Summary:

Java's ArrayList class provides the remove() function for removing elements from the collection. We can delete elements in ArrayList based on element value or index position. Use the remove() function to easily delete elements in ArrayList, making collection element management more flexible and efficient.

The above is an example of using the remove() function of the ArrayList class to delete elements in the collection. I hope this article can help you understand and use the remove() function provided by the ArrayList class.

The above is the detailed content of Java uses the remove() function of the ArrayList class to delete elements from the collection. 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