Given:
1. public class Drink implements Comparable{
2. public String name;
3. public int compareTo(Object o){
4. return 0;
5. }
6. }
and:
20. Drink one = new Drink();
21. Drink two = new Drink();
22. one.name = "Coffee";
23. two.name = "Tea";
24. TreeSet set = new TreeSet();
25. set.add(one);
26. set.add(two);
A programmer iterates over the TreeSet and prints the name of each Drink object.
What is the result?
A. Tea
B. Coffee
C. Coffee Tea
D. Compilation fails.
E. The code runs with no output.
F. An exception is thrown at runtime.
答案:B
參考:12-4-3 SortedSet集合
24. TreeSet set = new TreeSet(); //不可加入相同元素
25. set.add(one); //可加入
26. set.add(two); //視為同元素故不會加入,因為第3,4行
沒有留言:
張貼留言