public class Drink implements Comparable{
public String name;
public int compareTo(Object o){
return 0;
}
and:
Drink one=new Drink();
Drink two=new Drink();
one.name="Coffee";
two.name="Tea";
TreeSet set=new TreeSet();
set.add(one);
set.add(two);
A programmer iterates over the TreeSet and prints the name of each Drink object. what is the rusult?
A Tea
B.Coffe
C.Coffe Tea
D.Compilation fails.
E.the code runs with no output
F.An exception is thrown at runtime
/* public int compareTo(Object o){
return 0;
}
compareTo()返回0,表示两个对象相同. Set中不允许存在相同/重复的对象,后set()的对象如果和之前set()的对象相同(通过compareTo比较),那么将覆盖原来的对象所以Set中实际上只有一个Drink的实例 测试运行一下,结果为b,为什么不是a??,应该是b,add(one)起作用,add(two)的时候,经过判断,就不会加进去了应该!!所以用迭代器的时候,只有第一次加进去的!!!! */