nan*_*tek 1 java intellij-idea intellij-14
下面是 intellij 发出警告的代码片段Suspicious collections method calls
,但我不明白为什么。我唯一能想到的是,也许 intellij 认为其中一个列表可能为空,但这也会引发相同的错误。
这是 Intellij 的错误,还是真的有一些我没有想到的极端情况?
public class Foo {
public static void main(String[] args) {
List<String> foo = Arrays.asList("a", "b", "c");
List<String> bar = new ArrayList<>(foo);
bar.remove(foo); // Warning: 'List<String>' may not contain objects of type 'List<String>'
}
}
Run Code Online (Sandbox Code Playgroud)
public class Foo {
public static void main(String[] args) {
List<String> foo = Arrays.asList("a", "b", "c");
List<String> bar = new ArrayList<>(foo);
if (foo != null && bar !=null) {
bar.remove(foo); // Warning: 'List<String>' may not contain objects of type 'List<String>'
}
}
}
Run Code Online (Sandbox Code Playgroud)
Intellij 版本 2022.1.4 旗舰版
List<String> foo = Arrays.asList("a", "b", "c");
List<String> bar = new ArrayList<>(foo);
// bar.remove(foo); This is the same thing as:
bar.remove(Arrays.asList("a", "b", "c")); // still makes no sense.
// What would make sense:
bar.remove("a"); // remove the element "a"
bar.removeAll(foo); // remove all the elements in foo
Run Code Online (Sandbox Code Playgroud)
简而言之,在 a 中List<String>
,您通常会调用remove(String)
or removeAll(Collection<String>)
, not remove(List<String>)
,这不会真正执行您想要的操作。