public static void main(String[] args) { HashSet<String> s = new HashSet<String>(); s.add("A");
s.add("A");
s.add("B");
s.add(""); if (s.contains("B")) { s.add("B"); } System.out.println(s); }}
为什么我调试的时候if还执行?谢谢

解决方案 »

  1.   

    不明白啊。为什么你认为调试的时候,if不应该执行。
    你前面明明执行过一句:s.add("B");难道你希望s.contains("B")会返回false?
      

  2.   

    contains方法是如果此集合不包含指定元素,则返回 true啊
      

  3.   

    不用高人了,偶就可以解释了。- -b
    “contains方法是如果此集合不包含指定元素”——没有别的问题,你这句话不过是说反了而已。
      

  4.   

    以下是从JavaDoc拷贝出来的:contains
    public boolean contains(Object o)
    Returns true if this set contains the specified element. Specified by:
    contains in interface Collection<E>
    Specified by:
    contains in interface Set<E>
    Overrides:
    contains in class AbstractCollection<E>
    Parameters:
    o - element whose presence in this set is to be tested. 
    Returns:
    true if this set contains the specified element.
      

  5.   

    以下是从金山词霸中拷贝出来的 :Pcontain
    [kEn5tein]
    vt.
    包含, 容纳, 容忍
    vi.
    自制
    v.
    [军]牵制(敌军)
    v.
    [数]可被...除尽
      

  6.   

    contains
    public boolean contains(Object o)Returns 如果此集合不包含指定元素,则返回 true。 指定者:
    接口 Collection<E> 中的 contains
    指定者:
    接口 Set<E> 中的 contains
    覆盖:
    类 AbstractCollection<E> 中的 contains
    参数:
    o - 其在此集合中的存在已得到测试的元素。 
    返回:
    如果此集合不包含指定元素,则返回 true。
    这是jdk中的解释啊.
      

  7.   

    你可以给他们提交一个bug report了,呵呵。
      

  8.   

    contains
    public boolean contains(Object o)如果此 set 包含指定元素,则返回 true。 更确切地讲,当且仅当此 set 包含一个满足 (o==null ? e==null : o.equals(e)) 的 e 元素时,返回 true。 指定者:
    接口 Collection<E> 中的 contains
    指定者:
    接口 Set<E> 中的 contains
    覆盖:
    类 AbstractCollection<E> 中的 contains
    参数:
    o - 其在此 set 中的存在已得到测试的元素 
    返回:
    如果此 set 包含指定元素,则返回 true
    这是最新1.6的翻译
      

  9.   

    -  -! 1.6难道改正了这个BUG?