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


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


java.util.vector.removeAll(Collection col)方法用於從向量中刪除所有存在於指定集合中的元素。

用法:

Vector.removeAll(Collection col)

參數:此方法接受強製參數col,該參數是要從向量中刪除其元素的集合。


返回值:如果向量由於操作而改變,則此方法返回true,否則返回False。

異常:如果指定的集合為null,則該方法引發NullPointerException。

以下示例程序旨在說明Java.util.Vector.removeAll(Collection col)方法:

示例1:

// Java code to illustrate removeAll() 
import java.util.*; 
  
public class VectorDemo { 
    public static void main(String args[]) 
    { 
  
        // Creating an empty Vector 
        Vector<String> vec_tor = new Vector<String>(); 
  
        // Use add() method to add elements in the Vector 
        vec_tor.add("Geeks"); 
        vec_tor.add("for"); 
        vec_tor.add("Geeks"); 
        vec_tor.add("10"); 
        vec_tor.add("20"); 
  
        // Output the Vector 
        System.out.println("Vector: " + vec_tor); 
  
        // Creating an empty Vector 
        Vector<String> colvec_tor = new Vector<String>(); 
  
        // Use add() method to add elements in the Vector 
        colvec_tor.add("Geeks"); 
        colvec_tor.add("for"); 
        colvec_tor.add("Geeks"); 
  
        // Remove the head using remove() 
        boolean changed = vec_tor.removeAll(colvec_tor); 
  
        // Print the result 
        if (changed) 
            System.out.println("Collection removed"); 
        else
            System.out.println("Collection not removed"); 
  
        // Print the final Vector 
        System.out.println("Final Vector: " + vec_tor); 
    } 
}
輸出:
Vector: [Geeks, for, Geeks, 10, 20]
Collection removed
Final Vector: [10, 20]

示例2:

// Java code to illustrate removeAll() 
import java.util.*; 
  
public class VectorDemo { 
    public static void main(String args[]) 
    { 
  
        // Creating an empty Vector 
        Vector<Integer> vec_tor = new Vector<Integer>(); 
  
        // Use add() method to add elements in the Vector 
        vec_tor.add(1); 
        vec_tor.add(2); 
        vec_tor.add(3); 
        vec_tor.add(10); 
        vec_tor.add(20); 
  
        // Output the Vector 
        System.out.println("Vector: " + vec_tor); 
  
        // Creating an empty Vector 
        Vector<Integer> colvec_tor = new Vector<Integer>(); 
  
        // Use add() method to add elements in the Vector 
        colvec_tor.add(30); 
        colvec_tor.add(40); 
        colvec_tor.add(50); 
  
        // Remove the head using remove() 
        boolean changed = vec_tor.removeAll(colvec_tor); 
  
        // Print the result 
        if (changed) 
            System.out.println("Collection removed"); 
        else
            System.out.println("Collection not removed"); 
  
        // Print the final Vector 
        System.out.println("Final Vector: " + vec_tor); 
    } 
}
輸出:
Vector: [1, 2, 3, 10, 20]
Collection not removed
Final Vector: [1, 2, 3, 10, 20]


相關用法


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