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

List<String> names = ....
for (String name : names) {
   // Do something
   names.remove(name).
}

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

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

当前回答

“增强型for循环”的java设计是不向代码公开迭代器,但安全删除项的唯一方法是访问迭代器。所以在这种情况下,你得用老办法:

 for(Iterator<String> i = names.iterator(); i.hasNext();) {
       String name = i.next();
       //Do Something
       i.remove();
 }

如果在实际代码中,增强的for循环确实值得这样做,那么您可以将这些项添加到临时集合中,并在循环之后调用列表上的removeAll。

EDIT(重编):不,在迭代时以iterator.remove()方法之外的任何方式更改列表都会导致问题。解决这个问题的唯一方法是使用CopyOnWriteArrayList,但这实际上是为了解决并发问题。

删除重复项最便宜的方法(就代码行数而言)是将列表转储到LinkedHashSet中(如果需要,再转储回list中)。这样可以在删除重复项的同时保留插入顺序。

其他回答

我不知道迭代器,但是直到今天我才从循环中的列表中删除元素:

List<String> names = .... 
for (i=names.size()-1;i>=0;i--) {    
    // Do something    
    names.remove(i);
} 

这总是有效的,并且可以在其他不支持迭代器的语言或结构中使用。

是的你可以使用for-each循环, 要做到这一点,你必须维护一个单独的列表来保存删除项,然后使用removeAll()方法从名称列表中删除该列表,

List<String> names = ....

// introduce a separate list to hold removing items
List<String> toRemove= new ArrayList<String>();

for (String name : names) {
   // Do something: perform conditional checks
   toRemove.add(name);
}    
names.removeAll(toRemove);

// now names list holds expected values

Use

.remove() Interator或

Use

CopyOnWriteArrayList

for (String name : new ArrayList<String>(names)) {
    // Do something
    names.remove(nameToRemove);
}

克隆列表名称并在从原始列表中删除时遍历克隆名称。比上面的答案简洁一点。

当您想要从列表中删除元素时,最好使用迭代器

因为删除的源代码是

if (numMoved > 0)
    System.arraycopy(elementData, index+1, elementData, index,
             numMoved);
elementData[--size] = null;

所以,如果你从列表中删除一个元素,列表将被重构,其他元素的索引将被改变,这可能会导致你想要发生的事情。