我一直在使用Java/Python.现在在这种情况下,我想检查元素是否在列表中,并做了什么...
Python说:
if "a" in ["a", "b", "c"]:
print "It's there!"
Run Code Online (Sandbox Code Playgroud)
java是否为此提供任何一个内联,而不是逐步创建ArrayList/Set或类似的数据结构并向其中添加元素?
谢谢
Mar*_*iot 85
if( Arrays.asList("a","b","c").contains("a") )
Run Code Online (Sandbox Code Playgroud)
ray*_*d09 13
List接口中有一个boolean contains(Object obj)方法.
你应该可以说:
if (list.contains("a")) {
System.out.println("It's there");
}
Run Code Online (Sandbox Code Playgroud)
根据javadoc:
boolean contains(Object o)
Returns true if this list contains the specified element.
More formally, returns true if and only if this list contains at
least one element e such that (o==null ? e==null : o.equals(e)).
Run Code Online (Sandbox Code Playgroud)
在JDK7中:
if ({"a", "b", "c"}.contains("a")) {
Run Code Online (Sandbox Code Playgroud)
假设Project Coin集合文字项目通过.
编辑:它没有.