Home Java javaTutorial How to use the ArrayList.clear() method in Java to clear elements in a list?

How to use the ArrayList.clear() method in Java to clear elements in a list?

Nov 18, 2023 am 09:11 AM
java arraylist clear

How to use the ArrayList.clear() method in Java to clear elements in a list?

ArrayList in Java is a very commonly used data structure that allows us to dynamically store and manipulate data. In some cases, we may need to clear all elements in the list to refill or free up memory space. The ArrayList class provides a clear() method that can help us achieve this goal easily.

The clear() method is a member method of the ArrayList class, used to delete all elements in the list. This method is very simple, it does not receive any parameters and does not return any value. When we call the clear() method of the ArrayList object, it removes all elements from the list and resets the length of the list to 0.

The following is a sample code for clearing ArrayList:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

import java.util.ArrayList;

 

public class ClearArrayListExample {

    public static void main(String[] args) {

        // 创建一个ArrayList对象并添加一些元素

        ArrayList<String> list = new ArrayList<>();

        list.add("元素1");

        list.add("元素2");

        list.add("元素3");

 

        System.out.println("清空前的列表内容:" + list);

 

        // 调用clear()方法清空列表

        list.clear();

 

        System.out.println("清空后的列表内容:" + list);

    }

}

Copy after login

Run the above code, the output result is as follows:

1

2

清空前的列表内容:[元素1, 元素2, 元素3]

清空后的列表内容:[]

Copy after login

As can be seen from the results, the clear() method will All elements of are removed and the list becomes an empty list.

It should be noted that the clear() method only deletes the elements in the list, but does not release the memory space occupied by the ArrayList object. The reference to the ArrayList object itself still exists, just the list is empty. If you want to completely free the memory, you can set the ArrayList object to null or recreate a new ArrayList object.

In short, we can easily clear the elements in the list by calling the clear() method of ArrayList to prepare for subsequent operations. This method is very simple and easy to use, and is a commonly used method when we deal with ArrayList.

The above is the detailed content of How to use the ArrayList.clear() method in Java to clear elements in a list?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

How does Java's classloading mechanism work, including different classloaders and their delegation models?

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Mar 07, 2025 pm 06:09 PM

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?

How can I implement functional programming techniques in Java? How can I implement functional programming techniques in Java? Mar 11, 2025 pm 05:51 PM

How can I implement functional programming techniques in Java?

Node.js 20: Key Performance Boosts and New Features Node.js 20: Key Performance Boosts and New Features Mar 07, 2025 pm 06:12 PM

Node.js 20: Key Performance Boosts and New Features

Iceberg: The Future of Data Lake Tables Iceberg: The Future of Data Lake Tables Mar 07, 2025 pm 06:31 PM

Iceberg: The Future of Data Lake Tables

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?

See all articles