我有一个整数列表,其中包含多个值(比如200个)。
List<int> ExampleList;
还有一个整数列表,其中包含需要从ExampleList中删除的索引。但是,这个列表没有排序。
List<int> RemoveFromExampleList;
如果它被排序了,我会运行一个反向循环并删除所有这样的值:
for (int i = (RemoveFromExampleList.Count-1); i >=0; i--)
{
ExampleList.RemoveAt(RemoveFromExampleList[i]);
}
我需要对RemoveFromExampleList进行排序吗?或者有其他方法从ExampleList中删除不必要的值吗?
如果需要排序,最简单的方法是什么?是否有内置的C#库/方法可以排序?
RemoveFromExampleList.Sort();
这么简单。如果你在后面加上.Reverse()
,你就可以通过列表进行foreach
。 - Heinzi