Home > Java > javaTutorial > Clear the vector in Java using the removeAllElements() method of the Vector class

Clear the vector in Java using the removeAllElements() method of the Vector class

WBOY
Release: 2023-07-24 11:45:26
Original
1120 people have browsed it

Use the removeAllElements() method of the Vector class to clear the vector in Java

In Java programming, the Vector class is a dynamic array class that can add elements to the end of the array and automatically adjust the size. When we use the Vector class to save a large amount of data, sometimes we need to clear all elements in the vector. In this case, we can use the removeAllElements() method of the Vector class to implement the clearing operation.

The Vector class is part of the Java collection framework and is located in the java.util package. It provides many methods for manipulating vectors, including clearing operations. The removeAllElements() method implements a clearing operation by removing all elements in the vector.

The following is a sample code that uses the removeAllElements() method of the Vector class to clear the vector:

import java.util.Vector;

public class VectorDemo {

    public static void main(String[] args) {

        // 创建一个存放字符串的向量
        Vector<String> vector = new Vector<>();

        // 向向量中添加元素
        vector.add("Java");
        vector.add("Python");
        vector.add("C++");
        vector.add("JavaScript");

        System.out.println("清空前的向量内容:" + vector);

        // 使用removeAllElements()方法清空向量
        vector.removeAllElements();

        System.out.println("清空后的向量内容:" + vector);
    }
}
Copy after login

Run the above code, the following results will be output:

The vector content before clearing :[Java, Python, C, JavaScript]
Vector content after clearing: []

As you can see from the results, after clearing the vector by the removeAllElements() method, all elements in the vector are successfully deleted and the vector becomes empty.

It should be noted that the removeAllElements() method will directly modify the original vector, so after calling this method, the original vector will no longer contain any elements. If you want to create a new empty vector while retaining the original vector, you can use the parameterless constructor of Vector to achieve this:

Vector<String> newVector = new Vector<>();
Copy after login

The above is how to clear the vector in Java using the removeAllElements() method of the Vector class Introduction and sample code. I hope this article can help you understand and master this clearing operation.

The above is the detailed content of Clear the vector in Java using the removeAllElements() method of the Vector class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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