我正在使用Codingbat网站,特别是AP-1中的这种方法
public String[] wordsWithout(String[] words, String target) {
ArrayList<String> al = new ArrayList<>(Arrays.asList(words));
al.removeIf(s -> s.equals(target));
return al.toArray(new String[al.size()]);
}
Run Code Online (Sandbox Code Playgroud)
这个实现工作,以及它当前提交的内容,但是当我将return语句更改为
return al.toArray(String[]::new);
Run Code Online (Sandbox Code Playgroud)
据我所知,这应该工作,给出以下错误:
no suitable method found for toArray((size)->ne[...]size])
method java.util.Collection.<T>toArray(T[]) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; Array is not a functional interface))
method java.util.List.<T>toArray(T[]) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; Array is not a functional interface))
method java.util.AbstractCollection.<T>toArray(T[]) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; Array is not …Run Code Online (Sandbox Code Playgroud)