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?
A. The set variable contains all six elements from the coll collection, and the order is guaranteed to be
preserved.
B. The set variable contains only three elements from the coll collection, and the order is guaranteed to be
preserved.
C. The set variable contains all six elements from the coll collection, but the order is NOT guaranteed to be
preserved.
D. The set variable contains only three elements from the coll collection, but the order is NOT guaranteed
to be preserved.
答案:D
參考:12-4-1 Set集合、12-4-5 List集合
-----------------------------------------------------------------
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)
都會放入coll中共6個
12. Set set = new HashSet(coll); //一樣的不給放,所以只有三個可放入,但沒有順序的依據
沒有留言:
張貼留言