SCJP題庫第209題


Given:
1. import java.util.*;
2. public class TestSet{
3.   enum Example{ONE, TWO, THREE}
4.   public static void main(String[] args){
5.     Collection coll = new ArrayList();
6.     coll.add(Example.THREE);
7.     coll.add(Example.THREE);
8.     coll.add(Example.THREE);
9.     coll.add(Example.TWO);
10.   coll.add(Example.TWO);
11.   coll.add(Example.ONE)
12.   Set set = new HashSet(coll);
13.   }
14. }
Which statement is true about the set variable on line 12? 那一個敘述對於第12行的set變數為正確?
 
A. The set variable contains all six elements from the coll collection, and the order is guaranteed to be preserved. 
set變數包含6個從coll來的元素,且保證其排序的特性。

B. The set variable contains only three elements from the coll collection, and the order is guaranteed to be preserved.
set變數包含3個從coll來的元素,且保證其排序的特性。

C. The set variable contains all six elements from the coll collection, but the order is NOT guaranteed to be preserved.
set變數包含6個從coll來的元素,但不保證其排序的特性。

D. The set variable contains only three elements from the coll collection, but the order is NOT guaranteed to be preserved.
set變數僅包含從coll集合中的三個元素(物件),但不保證其排序的特性。

Ans: D
 解說: ArrayList集合物件可以加入重複的物件(無排序性質),此題的coll集合物件包含6個元素,但是將此coll集合物件做為HashSet集合物件的建立時,由於HashSet是不重複且不排序的性質,因此coll集合中的重覆元素不會被加到set這個集合物件中。


Comments

Popular posts from this blog

Android-使用webview在V3版的Google地圖GPS定位