java.util.Vector.removeAllElements()

Python/Java学习交流群:369606713


描述

所述removeAllElements()方法被用来从这个载体移除所有组件,并将其大小设置zero.This方法在功能到透明方法相同。

声明

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

public void removeAllElements()

参数

NA

返回值

NA

异常

NA

实例

以下示例显示了java.util.Vector.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<Integer>(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