Java Vector removeAllElements() 方法



描述

Java Vector removeAllElements() 方法用於移除此向量中的所有元件並將大小設定為零。此方法的功能與 clear 方法相同。

宣告

以下是 java.util.Vector.removeAllElements() 方法的宣告

public void removeAllElements()

引數

返回值

異常

移除整數向量所有元素的示例

以下示例演示瞭如何使用 Java Vector removeAllElements() 方法移除向量的所有元素。我們建立了一個整數向量,添加了一些元素,列印其大小,然後使用 removeAllElements() 方法移除所有元素。最後,我們列印了向量的新大小,以檢視是否所有元素都被移除。

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty Vector vec with an initial capacity of 4      
      Vector<Integer> vec = new Vector<>(4);

      // use add() method to add elements in the vector
      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);
      
      System.out.println("Size of the vector: "+vec.size());

      System.out.println("Removing all elements");
      
      // lets remove all the elements
      vec.removeAllElements();

      System.out.println("Now size of the vector: "+vec.size());      
   } 
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果。

Size of the vector: 4
Removing all elements
Now size of the vector: 0

移除字串向量所有元素的示例

以下示例演示瞭如何使用 Java Vector removeAllElements() 方法移除向量的所有元素。我們建立了一個字串向量,添加了一些元素,列印其大小,然後使用 removeAllElements() 方法移除所有元素。最後,我們列印了向量的新大小,以檢視是否所有元素都被移除。

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty Vector vec with an initial capacity of 4      
      Vector<String> vec = new Vector<>(4);

      // use add() method to add elements in the vector
      vec.add("D");
      vec.add("C");
      vec.add("B");
      vec.add("A");
      
      System.out.println("Size of the vector: "+vec.size());

      System.out.println("Removing all elements");
      
      // lets remove all the elements
      vec.removeAllElements();

      System.out.println("Now size of the vector: "+vec.size());      
   } 
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果。

Size of the vector: 4
Removing all elements
Now size of the vector: 0

移除學生向量所有元素的示例

以下示例演示瞭如何使用 Java Vector removeAllElements() 方法移除向量的所有元素。我們建立了一個學生物件向量,添加了一些元素,列印其大小,然後使用 removeAllElements() 方法移除所有元素。最後,我們列印了向量的新大小,以檢視是否所有元素都被移除。

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty Vector vec with an initial capacity of 4      
      Vector<Student> vec = new Vector<>(4);

      // use add() method to add elements in the vector
      vec.add(new Student(1, "Julie"));
      vec.add(new Student(2, "Robert"));
      vec.add(new Student(3, "Adam"));
      vec.add(new Student(4, "Jene"));
      
      System.out.println("Size of the vector: "+vec.size());

      System.out.println("Removing all elements");
      
      // lets remove all the elements
      vec.removeAllElements();

      System.out.println("Now size of the vector: "+vec.size());      
   } 
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
} 

輸出

讓我們編譯並執行以上程式,這將產生以下結果。

Size of the vector: 4
Removing all elements
Now size of the vector: 0
java_util_vector.htm
廣告