我正在寻找一种更好的模式来处理每个需要处理的元素列表,然后根据结果从列表中删除。
您不能在 foreach (var element in X)
中使用 .Remove(element)
(因为它会导致 Collection was modified; enumeration operation may not execute.
异常)...您也不能使用 for (int i = 0; i < elements.Count(); i++)
和 .RemoveAt(i)
因为它会破坏您在集合中的当前位置i
。
有没有一种优雅的方式来做到这一点?
使用 for 循环反向迭代您的列表:
for (int i = safePendingList.Count - 1; i >= 0; i--)
{
// some code
// safePendingList.RemoveAt(i);
}
例子:
var list = new List<int>(Enumerable.Range(1, 10));
for (int i = list.Count - 1; i >= 0; i--)
{
if (list[i] > 5)
list.RemoveAt(i);
}
list.ForEach(i => Console.WriteLine(i));
或者,您可以使用带有谓词的 RemoveAll method 来测试:
safePendingList.RemoveAll(item => item.Value == someValue);
这是一个简化的示例来演示:
var list = new List<int>(Enumerable.Range(1, 10));
Console.WriteLine("Before:");
list.ForEach(i => Console.WriteLine(i));
list.RemoveAll(i => i > 5);
Console.WriteLine("After:");
list.ForEach(i => Console.WriteLine(i));
foreach (var item in list.ToList()) {
list.Remove(item);
}
如果将“.ToList()”添加到列表(或 LINQ 查询的结果)中,则可以直接从“列表”中删除“项目”,而不会出现可怕的“集合已修改;枚举操作可能无法执行”。错误。编译器会制作“list”的副本,以便您可以安全地对数组进行删除。
虽然这种模式不是超级高效,但它具有自然的感觉并且对于几乎任何情况都足够灵活。例如,当您想将每个“项目”保存到数据库并仅在数据库保存成功时将其从列表中删除。
一个简单直接的解决方案:
使用标准的 for 循环在您的集合上运行 backwards 并使用 RemoveAt(i)
删除元素。
当您想在对集合进行迭代时从集合中删除元素时,首先应该想到反向迭代。
幸运的是,有一个比编写一个 for 循环更优雅的解决方案,它涉及不必要的输入并且容易出错。
ICollection<int> test = new List<int>(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
foreach (int myInt in test.Reverse<int>())
{
if (myInt % 2 == 0)
{
test.Remove(myInt);
}
}
Reverse<T>()
创建了一个向后遍历列表的迭代器,但它为它分配了与列表本身相同大小的额外缓冲区( referencesource.microsoft.com/#System.Core/System/Linq/… )。 Reverse<T>
不只是以相反的顺序遍历原始列表(不分配额外的内存)。因此 ToList()
和 Reverse()
具有相同的内存消耗(都创建副本),但 ToList()
不对数据做任何事情。使用 Reverse<int>()
,我想知道为什么列表颠倒了,出于什么原因。
Reverse<T>()
的实现创建了一个新缓冲区,这令人非常失望,我不太确定我是否理解为什么这是必要的。在我看来,根据Enumerable
的底层结构,至少在某些情况下应该可以在不分配线性内存的情况下实现反向迭代。
在泛型列表上使用 ToArray() 允许您在泛型列表上执行 Remove(item):
List<String> strings = new List<string>() { "a", "b", "c", "d" };
foreach (string s in strings.ToArray())
{
if (s == "b")
strings.Remove(s);
}
选择您想要的元素,而不是尝试删除您不想要的元素。这比删除元素要容易得多(通常也更有效)。
var newSequence = (from el in list
where el.Something || el.AnotherThing < 0
select el);
我想将此作为评论发布,以回应下面迈克尔·狄龙留下的评论,但它太长了,而且无论如何在我的回答中可能很有用:
就个人而言,我永远不会一个接一个地删除项目,如果您确实需要删除,然后调用 RemoveAll
它接受一个谓词并且只重新排列内部数组一次,而 Remove
对每个元素执行 Array.Copy
操作你删除。 RemoveAll
的效率要高得多。
而且当您向后迭代列表时,您已经有了要删除的元素的索引,因此调用 RemoveAt
会更有效率,因为 Remove
首先遍历列表以找到您尝试删除的元素的索引,但您已经知道该索引。
所以总而言之,我看不出有任何理由在 for 循环中调用 Remove
。理想情况下,如果可能的话,使用上面的代码根据需要从列表中流式传输元素,因此根本不需要创建第二个数据结构。
使用 .ToList() 将复制您的列表,如以下问题所述:ToList()-- Does it Create a New List?
通过使用 ToList(),您可以从原始列表中删除,因为您实际上是在迭代副本。
foreach (var item in listTracked.ToList()) {
if (DetermineIfRequiresRemoval(item)) {
listTracked.Remove(item)
}
}
如果确定要删除哪些项目的函数没有副作用并且不改变项目(它是一个纯函数),一个简单有效(线性时间)的解决方案是:
list.RemoveAll(condition);
如果有副作用,我会使用类似的东西:
var toRemove = new HashSet<T>();
foreach(var item in items)
{
...
if(condition)
toRemove.Add(item);
}
items.RemoveAll(toRemove.Contains);
这仍然是线性时间,假设哈希是好的。但是由于哈希集,它的内存使用量增加了。
最后,如果您的列表只是 IList<T>
而不是 List<T>
,我建议我对 How can I do this special foreach iterator? 的回答。与许多其他答案的二次运行时间相比,给定 IList<T>
的典型实现,这将具有线性运行时间。
由于任何删除都是在您可以使用的条件下进行的
list.RemoveAll(item => item.Value == someValue);
List<T> TheList = new List<T>();
TheList.FindAll(element => element.Satisfies(Condition)).ForEach(element => TheList.Remove(element));
您不能使用 foreach,但可以在删除项目时向前迭代并管理循环索引变量,如下所示:
for (int i = 0; i < elements.Count; i++)
{
if (<condition>)
{
// Decrement the loop counter to iterate this index again, since later elements will get moved down during the remove operation.
elements.RemoveAt(i--);
}
}
请注意,通常所有这些技术都依赖于被迭代的集合的行为。此处显示的技术适用于标准 List(T)。 (很可能编写自己的集合类和迭代器,它允许在 foreach 循环期间删除项目。)
在迭代该列表时在列表上使用 Remove
或 RemoveAt
故意变得困难,因为它几乎总是做错事。你也许可以用一些巧妙的技巧让它工作,但它会非常慢。每次调用 Remove
时,它都必须扫描整个列表以找到要删除的元素。每次调用 RemoveAt
时,它都必须将后续元素向左移动 1 个位置。因此,任何使用 Remove
或 RemoveAt
的解决方案都需要二次时间,O(n²)。
如果可以,请使用 RemoveAll
。否则,以下模式将在线性时间O(n)中就地过滤列表。
// Create a list to be filtered
IList<int> elements = new List<int>(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
// Filter the list
int kept = 0;
for (int i = 0; i < elements.Count; i++) {
// Test whether this is an element that we want to keep.
if (elements[i] % 3 > 0) {
// Add it to the list of kept elements.
elements[kept] = elements[i];
kept++;
}
}
// Unfortunately IList has no Resize method. So instead we
// remove the last element of the list until: elements.Count == kept.
while (kept < elements.Count) elements.RemoveAt(elements.Count-1);
我会从过滤掉您不想保留的元素的 LINQ 查询中重新分配列表。
list = list.Where(item => ...).ToList();
除非列表非常大,否则执行此操作不应该有明显的性能问题。
在迭代列表时从列表中删除项目的最佳方法是使用 RemoveAll()
。但是人们写的主要问题是他们必须在循环内做一些复杂的事情和/或有复杂的比较案例。
解决方案是仍然使用 RemoveAll()
但使用以下表示法:
var list = new List<int>(Enumerable.Range(1, 10));
list.RemoveAll(item =>
{
// Do some complex operations here
// Or even some operations on the items
SomeFunction(item);
// In the end return true if the item is to be removed. False otherwise
return item > 5;
});
对于这种情况,for 循环是一个糟糕的构造。
使用同时
var numbers = new List<int>(Enumerable.Range(1, 3));
while (numbers.Count > 0)
{
numbers.RemoveAt(0);
}
但是,如果您绝对必须使用 for
var numbers = new List<int>(Enumerable.Range(1, 3));
for (; numbers.Count > 0;)
{
numbers.RemoveAt(0);
}
或这个:
public static class Extensions
{
public static IList<T> Remove<T>(
this IList<T> numbers,
Func<T, bool> predicate)
{
numbers.ForEachBackwards(predicate, (n, index) => numbers.RemoveAt(index));
return numbers;
}
public static void ForEachBackwards<T>(
this IList<T> numbers,
Func<T, bool> predicate,
Action<T, int> action)
{
for (var i = numbers.Count - 1; i >= 0; i--)
{
if (predicate(numbers[i]))
{
action(numbers[i], i);
}
}
}
}
用法:
var numbers = new List<int>(Enumerable.Range(1, 10)).Remove((n) => n > 5);
但是,LINQ 已经有 RemoveAll()
来执行此操作
var numbers = new List<int>(Enumerable.Range(1, 10));
numbers.RemoveAll((n) => n > 5);
最后,您最好使用 LINQ 的 Where()
过滤和创建新列表,而不是改变现有列表。不变性通常是好的。
var numbers = new List<int>(Enumerable.Range(1, 10))
.Where((n) => n <= 5)
.ToList();
通过假设谓词是一个元素的布尔属性,如果它是真的,那么这个元素应该被删除:
int i = 0;
while (i < list.Count())
{
if (list[i].predicate == true)
{
list.RemoveAt(i);
continue;
}
i++;
}
我希望“模式”是这样的:
foreach( thing in thingpile )
{
if( /* condition#1 */ )
{
foreach.markfordeleting( thing );
}
elseif( /* condition#2 */ )
{
foreach.markforkeeping( thing );
}
}
foreachcompleted
{
// then the programmer's choices would be:
// delete everything that was marked for deleting
foreach.deletenow(thingpile);
// ...or... keep only things that were marked for keeping
foreach.keepnow(thingpile);
// ...or even... make a new list of the unmarked items
others = foreach.unmarked(thingpile);
}
这将使代码与程序员大脑中的过程保持一致。
Nullable<bool>
,如果您想允许未标记),然后在 foreach 之后使用它来删除/保留项目。
foreach(var item in list.ToList())
{
if(item.Delete) list.Remove(item);
}
只需从第一个列表创建一个全新的列表。我说“简单”而不是“正确”,因为创建一个全新的列表可能比以前的方法具有更高的性能(我没有打扰任何基准测试。)我通常更喜欢这种模式,它也可以用于克服Linq-To-Entities 限制。
for(i = list.Count()-1;i>=0;i--)
{
item=list[i];
if (item.Delete) list.Remove(item);
}
这种方式使用普通的旧 For 循环向后循环列表。如果集合的大小发生变化,向前执行此操作可能会出现问题,但向后执行应该始终是安全的。
在 C# 中,一种简单的方法是标记要删除的那些,然后创建一个新列表以进行迭代...
foreach(var item in list.ToList()){if(item.Delete) list.Remove(item);}
甚至更简单的使用 linq....
list.RemoveAll(p=>p.Delete);
但值得考虑的是其他任务或线程是否可以在您忙于删除的同时访问同一个列表,并且可能使用 ConcurrentList 代替。
只是想添加我的 2 美分以防万一这对任何人都有帮助,我遇到了类似的问题,但需要在迭代时从数组列表中删除多个元素。在我遇到错误并意识到在某些情况下索引大于数组列表的大小之前,最高支持的答案大部分都是为我做的,因为正在删除多个元素但循环的索引没有保留跟踪。我通过简单的检查解决了这个问题:
ArrayList place_holder = new ArrayList();
place_holder.Add("1");
place_holder.Add("2");
place_holder.Add("3");
place_holder.Add("4");
for(int i = place_holder.Count-1; i>= 0; i--){
if(i>= place_holder.Count){
i = place_holder.Count-1;
}
// some method that removes multiple elements here
}
这里有一个选项没有提到。
如果您不介意在项目中的某处添加一些代码,您可以添加和扩展 List 以返回一个类的实例,该实例会反向遍历列表。
你会像这样使用它:
foreach (var elem in list.AsReverse())
{
//Do stuff with elem
//list.Remove(elem); //Delete it if you want
}
这是扩展的样子:
public static class ReverseListExtension
{
public static ReverseList<T> AsReverse<T>(this List<T> list) => new ReverseList<T>(list);
public class ReverseList<T> : IEnumerable
{
List<T> list;
public ReverseList(List<T> list){ this.list = list; }
public IEnumerator GetEnumerator()
{
for (int i = list.Count - 1; i >= 0; i--)
yield return list[i];
yield break;
}
}
}
这基本上是 list.Reverse() 没有分配。
就像有些人提到的那样,您仍然会遇到逐个删除元素的缺点,如果您的列表很长,这里的一些选项会更好。但我认为有一个世界有人会想要 list.Reverse() 的简单性,而没有内存开销。
复制您正在迭代的列表。然后从副本中删除并插入原件。向后退会令人困惑,并且在并行循环时效果不佳。
var ids = new List<int> { 1, 2, 3, 4 };
var iterableIds = ids.ToList();
Parallel.ForEach(iterableIds, id =>
{
ids.Remove(id);
});
我会这样做
using System.IO;
using System;
using System.Collections.Generic;
class Author
{
public string Firstname;
public string Lastname;
public int no;
}
class Program
{
private static bool isEven(int i)
{
return ((i % 2) == 0);
}
static void Main()
{
var authorsList = new List<Author>()
{
new Author{ Firstname = "Bob", Lastname = "Smith", no = 2 },
new Author{ Firstname = "Fred", Lastname = "Jones", no = 3 },
new Author{ Firstname = "Brian", Lastname = "Brains", no = 4 },
new Author{ Firstname = "Billy", Lastname = "TheKid", no = 1 }
};
authorsList.RemoveAll(item => isEven(item.no));
foreach(var auth in authorsList)
{
Console.WriteLine(auth.Firstname + " " + auth.Lastname);
}
}
}
输出
Fred Jones
Billy TheKid
我发现自己处于类似情况,我必须删除给定 List<T>
中的每个 nth 元素。
for (int i = 0, j = 0, n = 3; i < list.Count; i++)
{
if ((j + 1) % n == 0) //Check current iteration is at the nth interval
{
list.RemoveAt(i);
j++; //This extra addition is necessary. Without it j will wrap
//down to zero, which will throw off our index.
}
j++; //This will always advance the j counter
}
从列表中删除一项的成本与要删除的项之后的项数成正比。在前半部分项目符合删除条件的情况下,任何基于单独删除项目的方法最终都必须执行大约 N*N/4 项复制操作,如果列表很大,这可能会变得非常昂贵.
一种更快的方法是扫描列表以找到要删除的第一个项目(如果有),然后从该点向前复制每个应该保留的项目到它所属的位置。完成此操作后,如果应保留 R 项,则列表中的前 R 项将是那些 R 项,所有需要删除的项都将在末尾。如果这些项目以相反的顺序被删除,系统最终将不必复制它们中的任何一个,因此如果列表中有 N 个项目,其中 R 个项目,包括所有前 F,被保留,则有必要复制 RF 项目,并将列表缩小一项 NR 倍。所有线性时间。
我的方法是首先创建一个索引列表,该列表应该被删除。然后我遍历索引并从初始列表中删除项目。这看起来像这样:
var messageList = ...;
// Restrict your list to certain criteria
var customMessageList = messageList.FindAll(m => m.UserId == someId);
if (customMessageList != null && customMessageList.Count > 0)
{
// Create list with positions in origin list
List<int> positionList = new List<int>();
foreach (var message in customMessageList)
{
var position = messageList.FindIndex(m => m.MessageId == message.MessageId);
if (position != -1)
positionList.Add(position);
}
// To be able to remove the items in the origin list, we do it backwards
// so that the order of indices stays the same
positionList = positionList.OrderByDescending(p => p).ToList();
foreach (var position in positionList)
{
messageList.RemoveAt(position);
}
}
使用属性跟踪要删除的元素,并在处理后将它们全部删除。
using System.Linq;
List<MyProperty> _Group = new List<MyProperty>();
// ... add elements
bool cond = false;
foreach (MyProperty currObj in _Group)
{
// here it is supposed that you decide the "remove conditions"...
cond = true; // set true or false...
if (cond)
{
// SET - element can be deleted
currObj.REMOVE_ME = true;
}
}
// RESET
_Group.RemoveAll(r => r.REMOVE_ME);
_Group.RemoveAll(condition(r))
有什么问题?
myList.RemoveAt(i--);
simples;
simples;
在这里做什么?
不定期副业成功案例分享
list.RemoveAll(Function(item) item.Value = somevalue)
RemoveAll()
所用的时间是反向for
循环的三倍。所以我肯定会坚持循环,至少在重要的部分。.Remove()
,使用foreach
是出现此错误的地方。在反向使用for
循环的同时使用RemoveAt(...)
可以让我们在跟踪索引的同时删除一个元素以避免超出范围。并且当使用RemoveAll()
时,它不会在循环中使用,因此无需担心修改集合本身,因为我们不会对其进行迭代。