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还执行?谢谢
s.add("A");
s.add("B");
s.add(""); if (s.contains("B")) { s.add("B"); } System.out.println(s); }}
为什么我调试的时候if还执行?谢谢
你前面明明执行过一句:s.add("B");难道你希望s.contains("B")会返回false?
“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.
[kEn5tein]
vt.
包含, 容纳, 容忍
vi.
自制
v.
[军]牵制(敌军)
v.
[数]可被...除尽
public boolean contains(Object o)Returns 如果此集合不包含指定元素,则返回 true。 指定者:
接口 Collection<E> 中的 contains
指定者:
接口 Set<E> 中的 contains
覆盖:
类 AbstractCollection<E> 中的 contains
参数:
o - 其在此集合中的存在已得到测试的元素。
返回:
如果此集合不包含指定元素,则返回 true。
这是jdk中的解释啊.
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的翻译