当前位置: 代码迷 >> J2SE >> list的remove解决方案
  详细解决方案

list的remove解决方案

热度:2679   发布时间:2013-02-25 00:00:00.0
list的remove
Java code
import java.util.*;public class TestList{      public static void main(String args[]){              List<String> li = new ArrayList<String>();                        li.add("1");                        li.add("2");                        li.add("3");                        li.add("4");                        li.add("5");                        li.add("6");                     for (String s : li) {                            li.remove(s);                        }                        for (int i = 0; i < li.size(); i++) {                                    System.out.println(li.get(i));                            }    } }

用增强的for循环,为什么会抛出ConcurrentModificationException的异常?

------解决方案--------------------------------------------------------
增强的for循环里面不能调remove()

如果要在loop里面remove:
Java code
for(Iterator<String> it=li.iterator();it.hasNext();){   it.next();   it.remove();}
------解决方案--------------------------------------------------------
在遍历的时候不能改变ArrayList,在遍历的时候进行修改就会报这个错
http://download.oracle.com/javase/1.5.0/docs/api/java/util/ConcurrentModificationException
上面是java-doc上的说明
------解决方案--------------------------------------------------------
太久没用java,忘记了,但是下面这段应该可以回答楼主的问题了:
The remove method removes the last element that was returned by next from the underlying Collection. The remove method may be called only once per call to next and throws an exception if this rule is violated.

Note that Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.

Use Iterator instead of the for-each construct when you need to:

* Remove the current element. The for-each construct hides the iterator, so you cannot call remove. Therefore, the for-each construct is not usable for filtering.
* Iterate over multiple collections in parallel. 

The following method shows you how to use an Iterator to filter an arbitrary Collection — that is, traverse the collection removing specific elements.

static void filter(Collection<?> c) {
for (Iterator<?> it = c.iterator(); it.hasNext(); )
if (!cond(it.next()))
it.remove();
}

------解决方案--------------------------------------------------------
集合中 在遍历一个List时不能remove 。。。。当遍历一个链表时,每个数的前后指针都是确定的 当remove一个数值时 可能使得这个链表断掉 从而造成遍历不能正常进行和结束 因此 在遍历时 是不允许remove的。。。
------解决方案--------------------------------------------------------
服了你了··s是字符串··要移除的话是选择的移除··比如li.remove(int i)
li.remove(s);

------解决方案--------------------------------------------------------
倒着遍历list 再remove。。。

------解决方案--------------------------------------------------------
要想改变list内容,用一般的for循环!
增强for循环只能迭代,不可修改List内容,具体原因看源码,
这是ArrayList中的add方法实现的源码,内部实现是采用数组实现的!而不是采用链表!
public boolean add(E e) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}

------解决方案--------------------------------------------------------
List是不能直接remove的。如果需要移除用迭代器!
------解决方案--------------------------------------------------------
呵呵 记得很多面试中都会问这个。。


------解决方案--------------------------------------------------------
迭代的时候,不是可以的。
------解决方案--------------------------------------------------------
  相关解决方案