内容是别人劳动成果,hl3292收集

1.这个我是用反循环来弄的。

for (int i = list.Count - 1; i >= 0; i--)
{
if (list[i].NO == item.NO)
{
list.RemoveAt(i);
}
}

网友评论:

用RemoveAll效率应该可以,他相当于一次遍历,把所有符合条件的元素都交换到List的前面,
然后一次性删除掉后面不符合条件的元素,效率是O(n)的,
如果是一条一条删,每删除一条之后都要把后面所有元素向前移动,效率是n^2的!

2.不过如果用其它list的话,其实也不用重现Clone方法
     

代码
public void RemoveItemFromList(ref List <A> list, A item)
{
List
<A> tempList = new List <A>();
foreach (A a in list)
{
if (a.NO != item.NO && !tempList.Contains(a))
tempList.Add(a);
}
list
= tempList;
}


这样也可以。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-26
  • 2021-08-27
  • 2022-12-23
  • 2021-08-20
  • 2022-12-23
  • 2021-09-04
猜你喜欢
  • 2021-12-26
  • 2022-01-24
  • 2022-12-23
  • 2021-12-26
  • 2021-12-02
  • 2021-06-29
相关资源
相似解决方案