位置:首頁 > Java技術 > Java.util包 > java.util.Vector.removeAllElements()方法實例

java.util.Vector.removeAllElements()方法實例

removeAllElements() 方法被用來從這個向量中取出所有組件,並設置其大小設置為零。此方法是在功能上與clear ()方法相同。

聲明

以下是java.util.Vector.removeAllElements()方法的聲明

public void removeAllElements()

參數

  • NA

返回值

NA

異常

  • NA

例子

下麵的例子顯示java.util.Vector.removeAllElements()方法的使用。

package com.yiibai;

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 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);

      // let us print all the elements available in vector
      System.out.println("Added numbers are :- "); 
      for (Integer number : vec) {         
         System.out.println("Number = " + number);
      }
      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());      
   } 
}

現在編譯和運行上麵的代碼示例,將產生以下結果。

Added numbers are :- 
Number = 4
Number = 3
Number = 2
Number = 1
Size of the vector: 4
Removing all elements
Now size of the vector: 0