【C#】遍历List列表的同时,移除访问到的元素
2017-04-12 09:42
681 查看
需求:遍历List列表,当访问的元素符合某一条件时,将该元素移除出列表。
注意点:使用foreach循环遍历无法做到边读边修改,所以要使用for循环。
例子:
重要参考:
http://stackoverflow.com/questions/1582285/how-to-remove-elements-from-a-generic-list-while-iterating-over-it
注意点:使用foreach循环遍历无法做到边读边修改,所以要使用for循环。
例子:
// 倒序遍历。 for (int i = list.Count - 1; i >= 0 ; i--) { if (list[i].name != null) { // list.Remove(list[i]); list.Remove(i); } }
重要参考:
http://stackoverflow.com/questions/1582285/how-to-remove-elements-from-a-generic-list-while-iterating-over-it
相关文章推荐
- C# 遍历List 并删除某个元素
- 【C#】通过遍历IFrame访问页面元素
- Remove Linked List Elements(移除列表中指定的元素)
- C#遍历集合移除元素
- C#遍历List并删除元素的方法概述
- 分析轮子(八)- List.java 各种遍历方式及遍历时移除元素的方法
- c#中List的元素遍历(foreach)和去重复(distinct)
- 遍历itemList是否含有listWidget列表里的item,有则移除
- 遍历List的同时清空元素
- 1.2.3 List.clear方法——从列表中移除所有元素
- C#遍历List并删除某个元素的方法
- 遍历List 同时 remove 元素
- C#遍历集合与移除元素的方法
- 循环遍历数组时同时移除不符合条件的元素
- 遍历List集合同时删除元素的正确方法
- c# IList.ToList()后更改元素值会不会影响原列表的值
- List 遍历时移除元素
- C#遍历List并删除某个或者几个元素的方法
- Python3基础 list 访问列表中的列表的元素
- C#对List同时遍历删除操作