? ? ? ? 本人能力有限,使用了一些Ai的結論,如有不足還請斧正?
目錄
1.HashSet <=> Dictionary
2.SortedSet <=>提供升序方法的List
3.ArrayList<=>List
4.BitArray <=> Bit[] array
5.StringCollection <=>List
6.StringDictionary<=>Dictionary
?
1.HashSet (可用)<=> Dictionary<onlyTKey,notUseTValue>
? ? ? ? 使用途徑:因為字典的查詢元素:Contains
? 平均時間復雜度為?(O(1)
? ? ? ? 所以一些場合將其代替List 但是普通字典又是鍵值對形式 所以不想要Value的時候可以使用HashSet
? ? ? ? 去除表中重復元素:
using System;
using System.Collections.Generic;class Program
{static void Main(){List<int> numbersWithDuplicates = new List<int> { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 };HashSet<int> uniqueNumbers = new HashSet<int>(numbersWithDuplicates);foreach (int number in uniqueNumbers){Console.WriteLine(number);}}
}
2.SortedSet (可用)<=>自己實現Sort的List
? SortedSet<T>
?同樣是一個存儲唯一元素的集合,但它會對元素進行自動排序
? ?如果除了元素唯一性,你還需要集合中的元素有序,那么?SortedSet<T>
?是不錯的選擇
using System;
using System.Collections.Generic;class Program
{static void Main(){SortedSet<int> sortedSet = new SortedSet<int>();sortedSet.Add(3);sortedSet.Add(1);sortedSet.Add(2);foreach (int num in sortedSet){Console.Write(num + " ");}Console.WriteLine();}
}
打印結果:1 2 3?
?3.BitArray(重要) <=> bool[] boolArray
? ? ? ? 學習過網絡的同學相信對這個東西并不陌生
????????BitArray
?是一個專門用于存儲位值的集合類,它以緊湊的方式存儲布爾值,每個布爾值僅占 1 位。這意味著在存儲大量布爾值時,BitArray
?占用的內存空間相對較小
? ? ? ? 其?提供了豐富的位操作方法,如?And
(按位與)、Or
(按位或)、Xor
(按位異或)、Not
(按位取反)等,方便進行復雜的位運算
????????支持動態調整大小,可以通過?Length
?屬性改變其長度
using System;
using System.Collections;class Program
{static void Main(){BitArray bitArray1 = new BitArray(new bool[] { true, false, true });BitArray bitArray2 = new BitArray(new bool[] { false, true, true });bitArray1.And(bitArray2); // 按位與操作for (int i = 0; i < bitArray1.Length; i++){Console.WriteLine(bitArray1[i]);}}
}
4.ArrayList(淘汰)<=>List<Object>
????????ArrayList是非泛型集合 其存儲的是Objcet? 所以每次存儲和獲取元素時都需要進行裝箱(值類型轉換為?object
?類型)和拆箱(object
?類型轉換為值類型)操作,這會帶來一定的性能開銷
? ? ? ? 因此,不建議使用但是仍然可以用
using System;
using System.Collections;class Program
{static void Main(){ArrayList logEntries = new ArrayList();logEntries.Add("Operation started");logEntries.Add(123);logEntries.Add(DateTime.Now);foreach (object entry in logEntries){if (entry is string){Console.WriteLine($"String: {entry}");}else if (entry is int){Console.WriteLine($"Integer: {entry}");}else if (entry is DateTime){Console.WriteLine($"Date: {entry}");}}}
}
5.StringDictionary((淘汰))<=>Dictionary<string,string>
StringDictionary
:StringDictionary
?繼承自?DictionaryBase
?類,實現了?IDictionary
、ICollection
?和?IEnumerable
?等非泛型接口。這意味著它是一個非泛型集合,在操作時需要進行類型轉換。Dictionary<string, string>
:Dictionary<string, string>
?是泛型集合,實現了?IDictionary<string, string>
、ICollection<KeyValuePair<string, string>>
?和?IEnumerable<KeyValuePair<string, string>>
?等泛型接口。泛型的使用使得代碼更加類型安全,無需進行顯式的類型轉換
? StringDictionary
?是大小寫不敏感的。也就是說,在?StringDictionary
?中,鍵?"Key"
?和?"key"
?被視為相同的鍵
using System;
using System.Collections.Specialized;class Program
{static void Main(){StringDictionary stringDict = new StringDictionary();stringDict.Add("Key", "Value1");stringDict["key"] = "Value2";Console.WriteLine(stringDict["Key"]); // 輸出: Value2}
}
? ? ? ? 但是也就僅限如此了?
?
?