相关疑难解决方法(0)

在Java中调用foreach循环中的remove

在Java中,使用foreach循环遍历集合时,对集合调用remove是否合法?例如:

List<String> names = ....
for (String name : names) {
   // Do something
   names.remove(name).
}
Run Code Online (Sandbox Code Playgroud)

作为附录,删除尚未迭代的项目是否合法?例如,

//Assume that the names list as duplicate entries
List<String> names = ....
for (String name : names) {
    // Do something
    while (names.remove(name));
}
Run Code Online (Sandbox Code Playgroud)

java foreach iterator loops

575
推荐指数
7
解决办法
41万
查看次数

标签 统计

foreach ×1

iterator ×1

java ×1

loops ×1