retainAll(Collection<?> c)%uA0方法用于仅保留此向量包含在指定Collection的元素。换言之,删除这个向量的所有元素未包含在指定Collection。
声明
以下是java.util.Vector.retainAll()方法的声明
public boolean retainAll(Collection<?> c)
参数
-
c--%uA0这对于在此Vector保留元素的集合。
返回值
如果此向量改变为调用的结果,方法调用返回true。
异常
-
NullPointerException--如果指定集合为null时抛出此异常。
例子
下面的例子显示java.util.Vector.retainAll()方法的使用。
package com.yiibai import java.util.Vector public class VectorDemo { public static void main(String[] args) { // create an empty Vector vec with an initial capacity of 7 Vectorvec = new Vector (7) Vector vecretain = new Vector (4) // use add() method to add elements in the vector vec.add(1) vec.add(2) vec.add(3) vec.add(4) vec.add(5) vec.add(6) vec.add(7) // this elements will be retained vecretain.add(5) vecretain.add(3) vecretain.add(2) System.out.println("Calling retainAll()") vec.retainAll(vecretain) // let us print all the elements available in vector System.out.println("Numbers after removal :- ") for (Integer number : vec) { System.out.println("Number = " + number) } } }
现在编译和运行上面的代码示例,将产生以下结果。
Calling retainAll() Numbers after removal :- Number = 2 Number = 3 Number = 5