foreach 改变集合时不能使用

使用foreach循环遍历list集合时,出现Collection was modified; enumeration operation may not execute.这个错误,查了半天才发现是当想要修改list集合时,不能使用foreach,因为foreach是取只读的,在取的时候数据不能变(包括修改,删除,添加等),所以只能用for循环。

参考:解决Collection was modified; enumeration operation may not execute异常

posted @ 2020-03-31 12:31  橘子香气  阅读(388)  评论(0编辑  收藏  举报