c# – 有效地从“foreach”中删除项目
发布时间:2020-12-15 03:48:03 所属栏目:百科 来源:网络整理
导读:现在,我能想到的最好的是: bool oneMoreTime = true;while (oneMoreTime){ ItemType toDelete=null; oneMoreTime=false; foreach (ItemType item in collection) { if (ShouldBeDeleted(item)) { toDelete=item; break; } } if (toDelete!=null) { collecti
现在,我能想到的最好的是:
bool oneMoreTime = true; while (oneMoreTime) { ItemType toDelete=null; oneMoreTime=false; foreach (ItemType item in collection) { if (ShouldBeDeleted(item)) { toDelete=item; break; } } if (toDelete!=null) { collection.Remove(toDelete); oneMoreTime=true; } } 我知道我在这里至少有一个额外的变量,但我把它包含在内,以提高算法的可读性. 解决方法
“RemoveAll”方法是最好的.
另一种常见的技术是: var itemsToBeDeleted = collection.Where(i=>ShouldBeDeleted(i)).ToList(); foreach(var itemToBeDeleted in itemsToBeDeleted) collection.Remove(itemToBeDeleted); 另一种常见的技术是使用“for”循环,但请确保您向后退: for (int i = collection.Count - 1; i >= 0; --i) if (ShouldBeDeleted(collection[i])) collection.RemoveAt(i); 另一种常见的技术是将未删除的项目添加到新集合中: var newCollection = new List<whatever>(); foreach(var item in collection.Where(i=>!ShouldBeDeleted(i)) newCollection.Add(item); 现在你有两个集合.一种我特别喜欢的技术是使用不可变数据结构.使用不可变数据结构,“删除”项不会更改数据结构;它会返回一个新的数据结构(如果可能,重新使用旧的数据结构),那些没有删除的项目.使用不可变的数据结构,您不会修改迭代的事情,所以没有问题: var newCollection = oldCollection; foreach(var item in oldCollection.Where(i=>ShouldBeDeleted(i)) newCollection = newCollection.Remove(item); 要么 var newCollection = ImmutableCollection<whatever>.Empty; foreach(var item in oldCollection.Where(i=>!ShouldBeDeleted(i)) newCollection = newCollection.Add(item); 当你完成后,你有两个集合.新的项目已删除,旧的是与以前相同的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |