简体   繁体   English

从列表中动态删除元素

[英]Dynamically removing elements from List

I am having an issue removing elements of a list while iterating through the list. 我在遍历列表时删除列表的元素时遇到问题。 Code: 码:

For (WebElement element: list){
    if (!element.isEnabled() || !element.isSelected()){
        list.remove(element);
    }
}

I get a ConcurrentModificationException , which I totally understand. 我得到了ConcurrentModificationException ,我完全理解。 I am removing an item from a list while in the loop that goes through the list. 我正在遍历列表的循环中从列表中删除项目。 Intuitively, that would screw up the indexing of the loop. 直观地讲,这会破坏循环的索引。

My question is, how else should I remove elements that are either not enabled or selected from this list? 我的问题是,我还应该如何从该列表中删除未enabled或未selected元素?

从循环列表中删除元素的最简单方法是使用ListIterator并使用例程iterator.remove()删除元素

Modifying a list while iterating through it, in a way outside of using the iterator, results in undefined behavior. 以迭代器之外的方式在迭代列表时修改列表会导致未定义的行为。 You'll have to use an iterator explicitly: 您必须显式使用迭代器:

Iterator<WebElement> iter = list.iterator();
while (iter.hasNext()) {
    WebElement element = iter.next();
    if (!element.isEnabled() || !element.isSelected()) {
        iter.remove();
    }
}

See this question for more. 有关更多信息,请参见此问题

Others have suggested using the list iterator. 其他人建议使用列表迭代器。 That has proven useful to me, but unfortunately it relies on a method, remove() , which is considered optional by the Iterable<E> interface. 事实证明,这对我很有用,但不幸的是,它依赖于方法remove() ,该方法在Iterable<E>接口中被认为是可选的。

Quoth the Javadoc, nevermore (emphasis mine): 不再使用Javadoc(重点是我):

void remove() 无效remove()

Removes from the underlying collection the last element returned by the iterator (optional operation) . 从基础集合中移除迭代器返回的最后一个元素(可选操作)

To get around that has proven more useful to me is a removal list. 要解决这个问题,对我来说更有用的是删除列表。

List<E> removed = new ArrayList<E>();
for(E element : list) {
    if(someCondition) removed.add(element);
}
list.removeAll(removed);

This has the added benefit of giving you a history of what you removed, just like the remove method does. 就像remove方法一样,这样做还有一个好处,即为您提供删除内容的历史记录。

The ConcurrentModificationException results from the fact that the for-each syntax is just syntactic sugar for using the Iterator interface. ConcurrentModificationException来自以下事实的结果:for-each语法只是使用Iterator接口的语法糖。

List iterators have what is known as the "fail-fast" attribute, meaning that any change made to the list aside from the interface provided by the iterator, immediately invalidates said iterator. 列表迭代器具有所谓的“快速失败”属性,这意味着除迭代器提供的接口之外,对列表进行的任何更改都会立即使所述迭代器无效。 Trying to use an invalidated iterator triggers your exception. 尝试使用无效的迭代器会触发您的异常。

@Claudiu has already posted this code, but for clarity, I will put it here as well. @Claudiu已经发布了此代码,但为清楚起见,我也将其放在此处。 In order to do what you're trying to do, you'll have to drop the fancy syntax and use a bare Iterator. 为了做您想做的事情,您必须删除花哨的语法并使用一个裸迭代器。

Iterator<WebElement iter = list.iterator();
while (iter.hasNext()) {
    WebElement element = iter.next();
    if (!element.isEnabled() || !element.isSelected()) {
        iter.remove();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM