📜  Java中的矢量 removeAllElements() 方法与示例

📅  最后修改于: 2022-05-13 01:54:36.579000             🧑  作者: Mango

Java中的矢量 removeAllElements() 方法与示例

Java.util.Vector.removeAllElements()方法用于从该 Vector 中删除所有组件并将其大小设置为零。

句法:

Vector.removeAllElements()

参数:该方法不带任何参数

返回值:该函数不返回任何值。

下面的程序说明了Java.util.Vector.removeAllElements() 方法。

示例 1:

// Java code to illustrate removeAllElements()
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector vector = new Vector();
  
        // Use add() method to add elements into the Vector
        vector.add("Welcome");
        vector.add("To");
        vector.add("Geeks");
        vector.add("4");
        vector.add("Geeks");
  
        // Displaying the Vector
        System.out.println("Vector: " + vector);
  
        // removeAllElementsing the Vector
        // using removeAllElements() method
        vector.removeAllElements();
  
        // Displaying the final Vector after removeAllElement
        System.out.println("The final Vector: " + vector);
    }
}
输出:
Vector: [Welcome, To, Geeks, 4, Geeks]
The final Vector: []

示例 2:

// Java code to illustrate removeAllElements()
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector vector = new Vector();
  
        // Use add() method to add elements into the Queue
        vector.add(10);
        vector.add(15);
        vector.add(30);
        vector.add(20);
        vector.add(5);
  
        // Displaying the Vector
        System.out.println("Vector: " + vector);
  
        // removeAllElementsing the Vector
        // using removeAllElements() method
        vector.removeAllElements();
  
        // Displaying the final Vector after removeAllElement
        System.out.println("The final Vector: " + vector);
    }
}
输出:
Vector: [10, 15, 30, 20, 5]
The final Vector: []