- Java.util 包类
- Java.util - 首页
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util 包附加内容
- Java.util - 接口
- Java.util - 异常
- Java.util - 枚举
- Java.util 有用资源
- Java.util - 有用资源
- Java.util - 讨论
Java Vector removeAll() 方法
描述
Java Vector removeAll(Collection<?> c) 用于移除向量中包含在指定集合中的所有元素。
声明
以下是java.util.Vector.removeAll() 方法的声明
public boolean removeAll(Collection<?> c)
参数
c − 这是要从向量中移除的元素集合。
返回值
如果此向量因调用而发生更改,则方法调用返回 true。
异常
NullPointerException − 如果指定的集合为 null,则抛出此异常。
移除整数向量中的多个元素示例
以下示例演示了 Java Vector removeAll(collection) 方法的用法。我们创建一个整数向量,添加一些元素,打印它,然后使用 removeAll(collection) 方法移除一些元素。由于向量已修改,因此会打印它以检查是否已移除指定的元素。
package com.tutorialspoint; import java.util.Vector; import java.util.Arrays; public class VectorDemo { public static void main(String[] args) { // create an empty vector Vector<Integer> vector = new Vector<>(); // use add() method to add elements in the vector vector.add(20); vector.add(15); vector.add(30); vector.add(45); // let us print all the elements available in vector System.out.println("Vector = " + vector); // it will remove two common elements System.out.println("Vector modified: " + vector.removeAll(Arrays.asList(11,30,20,12))); // let us print all the elements available in vector again System.out.println("Vector = " + vector); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
Vector = [20, 15, 30, 45] Vector modified: true Vector = [15, 45]
移除字符串向量中的多个元素示例
以下示例演示了 Java Vector removeAll(collection) 方法的用法。我们创建一个字符串向量,添加一些元素,打印它,然后使用 removeAll(collection) 方法移除一些元素。由于向量已修改,因此会打印它以检查是否已移除指定的元素。
package com.tutorialspoint; import java.util.Vector; import java.util.Arrays; public class VectorDemo { public static void main(String[] args) { // create an empty vector Vector<String> vector = new Vector<>(); // use add() method to add elements in the deque vector.add("A"); vector.add("B"); vector.add("C"); vector.add("D"); // let us print all the elements available in vector System.out.println("Vector = " + vector); // it will remove two common elements System.out.println("Vector modified: " + vector.removeAll(Arrays.asList("B","C","E"))); // let us print all the elements available in vector again System.out.println("Vector = " + vector); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
Vector = [A, B, C, D] Vector modified: true Vector = [A, D]
移除对象向量中的多个元素示例
以下示例演示了 Java Vector removeAll(collection) 方法的用法。我们创建一个学生对象向量,添加一些元素,打印它,然后使用 removeAll(collection) 方法移除一些元素。由于向量已修改,因此会打印它以检查是否已移除指定的元素。
package com.tutorialspoint; import java.util.Vector; import java.util.Arrays; public class VectorDemo { public static void main(String[] args) { // create an empty vector Vector<Student> vector = new Vector<>(); // use add() method to add elements in the deque vector.add(new Student(1, "Julie")); vector.add(new Student(2, "Robert")); vector.add(new Student(3, "Adam")); vector.add(new Student(4, "Jene")); // let us print all the elements available in vector System.out.println("Vector = " + vector); // it will remove two common elements System.out.println("Vector modified: " + vector.removeAll(Arrays.asList(new Student(1, "Julie"),new Student(3, "Adam")))); // let us print all the elements available in vector again System.out.println("Vector = " + vector); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
Vector = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ], [ 4, Jene ]] Vector modified: true Vector = [[ 2, Robert ], [ 4, Jene ]]
java_util_vector.htm
广告