C#中幾種常用的集合的用法
集合:將一推數據類型相同的數據放入到一個容器內,該容器就是數組:內存中開辟的一連串空間。
非泛型集合
ArrayList集合:
ArrayList是基於數組實現的,是一個動態數組,其容量能自動 增長
ArrayList的命名空間System.Collections
常用方法如下:
示例
static void Main(string[] args)
{ ArrayList list = new ArrayList(); //添加單個元素 list.Add(true); list.Add(1); list.Add("張三"); //添加集合元素 list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); //list.AddRange(list); //list.Clear();清空所有元素 //list.Remove(true);刪除單個元素 寫誰就刪出誰 //list.RemoveAt(0);根據下標去刪除元素 //list.RemoveRange(0, 3);根據下標去移除一定範圍的元素 //list.Insert(1, "插入的");在指定的位置插入一個元素 //list.InsertRange(0, new string[] { "張三", "李四" });在指定的位置插入一個集合 //bool b = list.Contains(1);判斷是否包含某個指定的元素 list.Add("大男孩"); if (!list.Contains("大男孩")) { list.Add("大男孩"); }else{ Console.WriteLine("已經有了"); }//循環打印 for (int i = 0; i < list.Count; i++) { Console.WriteLine(list[i]); } // list.Sort();//升序排列 //list.Reverse();反轉 Console.ReadKey(); }
ArrayList集合遍歷的三種方式:
foreach(Object item in list){
}
Ienumertor num=list.GetEnumertor();
while(num.MoveNext()){
Console.writer(num.Curret.ToString()+"");
}
for(int i=0;i<list.count;i++){
Console.Writer(list[i].ToString()+"");
}
HashTable集合:
HashTable的常用屬性和方法:
示例:
//創建一個HashTableHashtable openWith = new Hashtable();
//為HashTable添加元素,不能有重復的key,但可以有重復的值
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
//通過key獲得值
Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
//重新賦值
openWith["rtf"] = "winword.exe";
Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
//判斷是否包含特定的key
if (!openWith.ContainsKey("ht"))
{
openWith.Add("ht", "hypertrm.exe");
Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]);
}
HashTable集合遍歷的方式:
泛型集合
List<T>集合:
特點:
1)、可通過索引訪問的對象的強類型。
2)、是ArrayList類的泛型等效類。
3)、可以使用一個整數索引訪問此集合中的元素;索引從 零開始。
4)、可以接收null空引用(VB中的Nothing)。
5)、允許重復元素。
List<T>集合的方法:
01. Add 將對象添加到 List<T> 的結尾處。
02. AddRange 將指定集合的元素添加到 List<T> 的末尾。
03. AsReadOnly 返回當前集合的只讀 IList<T> 包裝。
04. BinarySearch(T) 使用默認的比較器在整個已排序的 List<T> 中搜索元素,並返回該元素從零開始的索引。
05. BinarySearch(T, IComparer<T>) 使用指定的比較器在整個已排序的 List<T> 中搜索元素,並返回該元素從零開始的索引。
06 . BinarySearch(Int32, Int32, T, IComparer<T>) 使用指定的比較器在已排序 List<T> 的某個元素範圍中搜索元素,並返回該元素從零開始的索引。
07. Clear 從 List<T> 中移除所有元素。
08. Contains 確定某元素是否在 List<T> 中。
09. ConvertAll<TOutput> 將當前 List<T> 中的元素轉換為另一種類型,並返回包含轉換後的元素的列表。
10. CopyTo(T[]) 將整個 List<T> 復制到兼容的一維數組中,從目標數組的開頭開始放置。
11. Exists 確定 List<T> 是否包含與指定謂詞所定義的條件相匹配的元素。
12. Find 搜索與指定謂詞所定義的條件相匹配的元素,並返回整個 List<T> 中的第一個匹配 元素。
13 .FindIndex(Predicate<T>) 搜索與指定謂詞所定義的條件相匹配的元素,並返回整個List <T> 中第一個匹配元素的從零開始的索引。
14. ForEach 對 List<T> 的每個元素執行指定操作。 GetEnumerator 返回循環訪問 List<T> 的枚舉器。
15 . IndexOf(T) 搜索指定的對象,並返回整個 List<T> 中第一個匹配項的從零開始的索引。
16. Insert 將元素插入 List<T> 的指定索引處。
17. InsertRange 將集合中的某個元素插入 List<T> 的指定索引處。
18. LastIndexOf(T) 搜索指定的對象,並返回整個 List<T> 中最後一個匹配項的從零開始的索引。
19. Remove 從 List<T> 中移除特定對象的第一個匹配項。
20. Reverse() 將整個 List<T> 中元素的順序反轉。
21. Sort() 使用默認比較器對整個 List<T> 中的元素進行排序。
List<T>集合遍歷的二種方法:
Dictionary<K,V>集合:
常用的方法和屬性:
特點:
從一組鍵(Key)到一組值(Value)的映射,每一個添加項都是由一個值及其相關連的鍵組成
-
任何鍵都必須是唯一的
鍵不能為空引用null(VB中的Nothing),若值為引用類型,則可以為空值
Key和Value可以是任何類型(string,int,custom class 等)
-
Comparer: 獲取用於確定字典中的鍵是否相等的 IEqualityComparer。
Count: 獲取包含在 Dictionary中的鍵/值對的數目。
Item: 獲取或設置與指定的鍵相關聯的值。
Keys: 獲取包含 Dictionary中的鍵的集合。
Values: 獲取包含 Dictionary中的值的集合。
Add: 將指定的鍵和值添加到字典中。
Clear: 從 Dictionary中移除所有的鍵和值。
ContainsKey: 確定 Dictionary是否包含指定的鍵。
ContainsValue: 確定 Dictionary是否包含特定值。
GetEnumerator: 返回循環訪問 Dictionary的枚舉數。
GetType: 獲取當前實例的 Type。 (從 Object 繼承。)
Remove: 從 Dictionary中移除所指定的鍵的值。
ToString: 返回表示當前 Object的 String。 (從 Object 繼承。)
TryGetValue: 獲取與指定的鍵相關聯的值。
-
創建及初始化
Dictionary<int,string>myDictionary=newDictionary<int,string>();
添加元素
myDictionary.Add(1,"C#");
myDictionary.Add(2,"C++");
myDictionary.Add(3,"ASP.NET");
myDictionary.Add(4,"MVC");
-
通過Key查找元素
if(myDictionary.ContainsKey(1))
{
Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]);
}
通過Remove方法移除指定的鍵值
myDictionary.Remove(1);
if(myDictionary.ContainsKey(1))
...{
Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]);
}
else
{
Console.WriteLine("不存在 Key : 1");
}
Dictionary<T>集合遍歷的方式:
-
通過KeyValuePair遍歷元素
foreach(KeyValuePair<int,string>kvp in myDictionary)
...{
Console.WriteLine("Key = {0}, Value = {1}",kvp.Key, kvp.Value);
}
僅遍歷鍵 Keys 屬性
Dictionary<int,string>.KeyCollection keyCol=myDictionary.Keys;
foreach(intkeyinkeyCol)
...{
Console.WriteLine("Key = {0}", key);
}
僅遍歷值 Valus屬性
Dictionary<int,string>.ValueCollection valueCol=myDictionary.Values;
foreach(stringvalueinvalueCol)
...{
Console.WriteLine("Value = {0}", value);
}
1)、Dictionary<K,V> students=new Dictionary<K,V>(); 其中“K”為占位符,具體定義時用存儲鍵“Key”的數據類型代替,“V”也是占位符,用元素的值“Value”的數據類型代替,這樣就在定義該集合時,聲明了存儲元素的鍵和值的數據類型,保證了類型的安全性。
2)、Dictionary<K,V>中元素的操作方法與HashTable相似,添加元素,獲取元素,刪除元素,遍歷集合元素的方法基本相同。
3)、Dictionary<K,V>和HashTable的區別 Dictionary<K,V>和HashTable的相同點: 添加元素,刪除元素,通過鍵訪問值的方法相同。
Dictionary<K,V>和HashTable的不同點: Dictionary<K,V>對添加的元素具有類型約束,HashTable可添加任意類型的元素。
Dictionary<K,V>不需要裝箱、拆箱操作,HashTable添加時裝箱,讀取時拆箱。
ArrayList與List<T>的區別:
HashTable與Dictionary<K,T>的區別:
C#中幾種常用的集合的用法