[英]Java declaring a String array in method invocation
I'm trying to declare a String array within a method invocation, like so: 我试图在方法调用中声明一个String数组,如下所示:
if (emailYaml.keySet().containsAll(new String[]{"mailto","subject","text"}))
I'm not entirely sure if this is the best approach in any case (i'm pretty new to programming), but in any case Eclipse tells me: 我不完全确定这是否是最好的方法(我对编程很新),但无论如何Eclipse都告诉我:
The method containsAll(Collection<?>) in the type Set<String> is not applicable for the arguments (String[])
Could anyone help with a solution? 任何人都可以帮助解决方案?
Many thanks 非常感谢
An array isn't a subtype of Collection
. 数组不是Collection
的子类型。 Convert it to a List
for example, before passing it to containsAll
: 例如,在将其传递给containsAll
之前将其转换为List
:
.containsAll(Arrays.asList(new String[] {"mailto", "subject", "text"}))
Or even simplier, since asList
takes a vararg as parameter: 甚至更简单,因为asList
将vararg作为参数:
.containsAll(Arrays.asList("mailto", "subject", "text"))
containsAll()
expects a Collection
and you are passing an array, thus your compiler complains. containsAll()
需要一个Collection
并且你传递一个数组,因此你的编译器会抱怨。
if (emailYaml.keySet().containsAll(Arrays.asList(new String[]
{"mailto","subject","text"})))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.