我有一个List
,即[0.1, 0.3, 0.8, 0.3, 0.4, 0.7, 0.9, 0.5]
。如何通过提供要从原始列表(即RemoveRange
)中删除的索引列表来执行[0, 4, 2, 7, 8]
?
在我的情况下,List(T).RemoveRange
不能工作,因为它被定义为
public void RemoveRange(int index, int count)
另外,我不希望使用for
循环并迭代地检查列表。还有别的办法吗?
发布于 2015-12-03 16:02:32
您可以使用LINQ的Enumerable.Where
保存所有不在索引列表中的内容:
list = list.Where((d, index) => !indices.Contains(index)).ToList();
另一种“不太优雅”的方法是使用反向循环和List.RemoveAt
。
indices.Sort();
for (int i = indices.Count - 1; i >= 0; i--)
list.RemoveAt(indices[i]);
https://stackoverflow.com/questions/34070581
复制相似问题