Java 使用删除循环列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1921104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
loop on list with remove
提问by enguerran
for (String fruit : list)
{
if("banane".equals(fruit))
list.remove(fruit);
System.out.println(fruit);
}
Here a loop with remove instruction. At execution time, I get some ConcurrentModificationException, below the console output:
这是一个带有删除指令的循环。在执行时,我在控制台输出下方得到一些 ConcurrentModificationException:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
at java.util.AbstractList$Itr.next(AbstractList.java:420)
at Boucle.main(Boucle.java:14)
abricot
banane
Question: How to remove some element with a loop?
问题:如何用循环删除一些元素?
采纳答案by Jon Skeet
You need to use the iterator directly, and remove the item via that iterator.
您需要直接使用迭代器,并通过该迭代器删除项目。
for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
String fruit = iterator.next();
if ("banane".equals(fruit)) {
iterator.remove();
}
System.out.println(fruit);
}
回答by Frederik Gheysels
Use a for loop, and loop over the collection in a reverse order. (That means, start with the last element, and loop to the first element. By doing so, you won't get problems by the indexes that change because of removing elements from the collection.
使用 for 循环,并以相反的顺序循环遍历集合。(这意味着,从最后一个元素开始,然后循环到第一个元素。这样做,您不会因为从集合中删除元素而更改索引而遇到问题。
You get the exception in the example that you post, because the list over which your iterator iterates, has changed, which means that the iterator becomes invalid.
您在发布的示例中遇到异常,因为迭代器迭代的列表已更改,这意味着迭代器无效。
回答by Itay Maman
for(Iterator<String> iter = list.iterator(); iter.hasNext(); )
{
String fruit = iter.next();
if("banana".equals(fruit))
iter.remove();
System.out.println(fruit);
}
回答by Bombe
In addition to using the Iterator
directly (which I would recommend) you can also store elements that you want to remove in a different list.
除了Iterator
直接使用(我推荐)之外,您还可以将要删除的元素存储在不同的列表中。
List<String> toRemove = new ArrayList<String>();
for (String fruit : list) {
if ("banane".equals(fruit))
toRemove.add(fruit);
System.out.println(fruit);
}
for (String fruit : toRemove) {
list.remove(fruit);
}
Mind you, I do not recommend this, it's just an alternative. :)
请注意,我不推荐这样做,它只是一种替代方法。:)
回答by Tom Neyland
Similar to what Bombe suggested, but in less lines of code by iterating on the list copy, but removing from the original list;
与 Bombe 建议的类似,但通过迭代列表副本减少了代码行,但从原始列表中删除;
List<String> temp = new ArrayList<String>(list);
for (String fruit : temp)
{
if("banane".equals(fruit))
list.remove(fruit);
System.out.println(fruit);
}
Personally I think this looks nicer than iterating with an iterator.
我个人认为这看起来比用迭代器迭代更好。
回答by steaiii
This seems a bit complicated, why not just do a normal for loop? I think it looks cleaner and won't throw this error. Just decriment i if you remove something. at least mine works, anyway. Those sort of auto-loops are more for coding convenience, I thought, so if they aren't being convenient then just don't use them.
这看起来有点复杂,为什么不做一个普通的 for 循环呢?我认为它看起来更干净,不会抛出这个错误。如果你删除了一些东西,就贬低 i 。至少我的作品,无论如何。我想,这些自动循环更多是为了编码方便,所以如果它们不方便,那就不要使用它们。
for (int i = list.size() - 1; i>=0; i--) {
String fruit = list.get(i);
System.out.println(fruit);
if ("banane".equals(fruit)) {
list.remove(fruit);
}
}
回答by Jawad Zeb
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
Iterator<String> iter = list.iterator();
while (iter.hasNext()) {
String s = iter.next();
if (s.equals("a")) {
iter.remove();
}
}
is the best approach..
是最好的方法..