當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.util.Vector.removeAll()用法及代碼示例


描述

這個removeAll(Collection<?> c)用於刪除指定集合中包含的向量的所有元素。

聲明

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

public boolean removeAll(Collection<?> c)

參數

c─ 這是要從 Vector 中刪除的元素的集合

返回值

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

異常

NullPointerException- 如果指定的集合為空,則拋出此異常。

示例

下麵的例子展示了 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

相關用法


注:本文由純淨天空篩選整理自 java.util.Vector.removeAll() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。