java.util.Vector.removeAll()


描述

所述removeAll(Collection<?> c)中用于去除包含在指定Collection中的向量的所有元素。

声明

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

public boolean removeAll(Collection<?> c)

参数

c - 这是要从Vector中删除的元素的集合

返回值

如果此Vector因调用而更改,则方法调用返回true。

异常

NullPointerException - 如果指定的collection为null,则抛出此异常。

实例

以下示例显示了java.util.Vector.removeAll()方法的用法。

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

      // let us remove all the elements
      System.out.println("Removing all the elements");
      vec.removeAll(vec);
      System.out.println("Size of the vector: "+vec.size());       
   }     
}

让我们编译并运行上面的程序,这将产生以下结果。

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