[英]C# Remove a list from a “jagged” list
我正在嘗試從List<List<int>>
刪除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));
您需要刪除要刪除的確切對象,而不是新對象(這將是一個不同的對象)。 例如:
my2DList.Remove(my2DList[3]);
如果您不知道索引或對象,則需要遍歷List以查找正確的對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.