,我到处都找了,还没有找到解决办法。 这是我到目前为止尝试过的: List> my2DList = ... ; // this is where I assi-6ren">
gpt4 book ai didi

C# 从 "jagged"列表中删除列表

转载 作者:行者123 更新时间:2023-11-30 13:35:33 25 4
gpt4 key购买 nike

我正在尝试删除 List<int>来自List<List<int>> ,我到处都找了,还没有找到解决办法。

这是我到目前为止尝试过的:

List<List<int>> my2DList = ... ;  // this is where I assign my 2D list
my2DList.Remove(new List<int>( ... ));

但是my2DList的长度保持不变。我该怎么办?

最佳答案

问题是 List<int>不会覆盖 Equals/GetHashCode ,因此您的新列表永远不会等于现有列表。 (基本上,它将比较引用文献而不是内容。)

三个选项:

  • 找到您要删除的确切列表,并将该引用传递给 Remove
  • 找到您要删除的列表的索引,并将其传递给RemoveAt
  • 创建谓词并使用RemoveAll

最后一个例子:

List<int> listToRemove = new List<int> { ... };
my2DList.RemoveAll(x => x.SequenceEqual(listToRemove));

关于C# 从 "jagged"列表中删除列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50888380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com