看板 java 關於我們 聯絡資訊
class A{ int a=0; public A(int va){a=va;} public boolean equals(Object o){ A m=(A)o; return (a==o.a); } public String toString(){...return a 的String} } class test{ main(String[] argc) { HashSet<A> set= new HashSet<A>(); set.add(new A(1)); set.add(new A(2)); System.out.println(set); } } 結果印出來是 [1, 2] 第二次被成功加入了 可是SUN 的 Spec上明明定義set 重複的不會加入 More formally, adds the specified element, o, to this set if this set contains no element e such that (o==null ? e==null : o.equals(e)). 可是現在看來 好像只有把address相同的才看成一樣 可是我的equals也overrinding了 也測過沒錯 我哪裡弄錯了嗎 請教板友 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.107.75