我創建了一個程序,在其中我有INT陣列的隊列:當在隊列中使用Contains時,控制檯返回False。爲什麼?
Queue<int[]> Test = new Queue<int[]>();
鑑於陣列,我想要查詢的陣列是否在隊列內。
我目前使用的命名空間是:System,System.Collections和System.Collections.Generic。
我想是這樣的:
Queue<int[]> Test = new Queue<int[]>();
Test.Enqueue(new int[] { 20, 20 });
Console.WriteLine(Test.Contains(new int[] { 20, 20 })); // Is the array {20,20} inside the queue?
在這段代碼中,在我看來,控制檯會輸出「真」,因爲我加入了數組{20,20}到隊列中,而現在我正在使用Contains方法進行檢查。
但是當我運行代碼 - 控制檯輸出false。
我有兩個問題: 這是爲什麼發生?我該如何解決這個問題?
比較適用於引用,您需要創建相等比較器或循環遍歷每個數組並調用SequenceEqual並傳遞該新數組以進行比較。 – terrybozzio 2014-10-05 17:16:34