java vector search_java.util.Vector.retainAll()方法实例
全屏
retainAll(Collection> c)方法用于僅保留此向量包含在指定Collection的元素。換言之,刪除這個向量的所有元素未包含在指定Collection。
聲明
以下是java.util.Vector.retainAll()方法的聲明public?boolean?retainAll(Collection>?c)
參數c--?這對于在此Vector保留元素的集合。
返回值
如果此向量改變為調用的結果,方法調用返回true。
異常NullPointerException--如果指定集合為null時拋出此異常。
例子
下面的例子顯示java.util.Vector.retainAll()方法的使用。package?cn.sxt;
import?java.util.Vector;
public?class?VectorDemo?{
public?static?void?main(String[]?args)?{
//?create?an?empty?Vector?vec?with?an?initial?capacity?of?7
Vector?vec?=?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
分享到:
0評論
總結
以上是生活随笔為你收集整理的java vector search_java.util.Vector.retainAll()方法实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 养生堂的维生素E含量纯吗?其含量是多少?
- 下一篇: 关于海的个性签名大全