前言html
本節主要是來了解學習集合,以方便在程序編寫時,什麼地方該選用什麼集合,讓程序更健壯的運行起來。在學習瞭解集合以前,首先須要瞭解一些數據結構方面的知識。下面咱們就先簡單的來看一下數據結構。算法
數據結構編程
數據結構就是相互之間存在一種或多種特定關係的數據元素的集合。 程序界有一點很經典的話,程序設計=數據結構+算法。用源代碼來體現,數據結構,就是編程。數組
集合分類數據結構
在上圖中能夠看到,集合整體上分爲線性集合和非線性集合。線性集合按照存儲方式又分爲直接存儲和順序存儲。ide
直接存儲,是指該類型的集合數據元素能夠直接經過下標(即index)來訪問,在C#中直接存儲的數據結構有三類:Array(包含數組和List<T>)、string、struct。學習
直接存儲結構的優勢是:向數據結構中添加元素是很高效的,直接放在數據末尾的第一個空位上就能夠了。測試
直接存儲結構的缺點是:向集合插入元素將會變得低效,它須要給插入的元素騰出位置並順序移動後面的元素。spa
順序存儲結構,即線性表。線性表可動態的擴大和縮小,它在一片連續的區域中存儲數據元素。線性表不能按照索引進行查找,它是經過對地址的引用來搜索元素的,爲了找到某個元素,它必須遍歷全部元素,直到找到對應的元素爲止。因此,線性表的優勢是插入和刪除數據效率高,缺點是查找的效率相對來講低一點。設計
線性表有能夠分爲隊列、棧以及索引羣集,在C#中分別表現爲:Queue<T>、Stack<T>,索引羣集又進一步泛化爲字典類型Dictionary<TKey,TValue>和雙向鏈表LinkedList<T>。
非線性集合本身在實際應用中比較少,並且感受也比較複雜,因此在此先不作討論學習。下面咱們就來一一的學習一下平常使用比較頻繁的集合吧。
數組
數組就是包含同一類型的多個元素。
數組的聲明:int[] intArray;
注意:數組聲明時,方括號([])必須跟在類型的後面,而不是變量名的後面。在C#中,將方括號放在變量名後是不合法的語法。
數組的初始化:
咱們知道數組是引用類型,因此須要給他們分配堆上的內存。
一、intArray=new int[3];
二、intArray=new int[]{1,2,3};
三、int[] intArray={1,2,3};
數組在聲明和初始化後,可使用索引器進行訪問,索引器老是以0開頭,表示第一個元素。
多維數組:
通常能夠是一維數組,二維數組、三維數組、多維數組。下面簡單的來看一下數組吧
///最簡單的一維數組 int[] intArray = { 1, 2, 3 }; ///二維數組,兩行三列(相似X軸和Y軸平面幾何) int[,] intTwoArray=new int[2,3]; intTwoArray[0, 0] = 0; intTwoArray[0, 1] = 1; intTwoArray[0, 2] = 2; intTwoArray[1, 0] = 3; intTwoArray[1, 1] = 4; intTwoArray[1, 2] = 5; ///二維數組,三行四列 int[,] intTwoArray2 = new int[,] { { 1, 11, 111 }, { 2, 22, 222 }, { 3, 33, 333 }, { 4, 44, 444 } }; ///三維數組,三行三列 int[, ,] intThreeArray; intThreeArray = new int[,,] { { {1,1}, {11,11}, {111,111} }, { {2,2}, {22,22}, {222,222} }, { {3,3}, {33,33}, {333,333} }, { {4,4}, {44,44}, {444,444} } };
上面簡單的介紹說明了一下一維數組、二維數組和三維數組。
ArrayList
ArrayList類繼承瞭如下幾個接口
public class ArrayList : IList, ICollection, IEnumerable, ICloneable
如下來看一下ArrayList的基本操做
///定義一個一維數組 int[] intArray = { 1, 2, 3 }; ///直接將一維數組轉換爲ArrayList; ArrayList array = new ArrayList(intArray); ///Add方法 array.Add(5); ///Insert方法兩個參數,索引位置和值 array.Insert(3, 7); ////刪除元素 ///直接刪除Array中的指定元素 array.Remove(3); ////刪除索引位置爲3的元素 array.RemoveAt(3); ////刪除一個範圍,從Array中索引爲1的元素開始刪除三個元素 array.RemoveRange(1, 3); ///Array的遍歷 foreach (int i in intArray) { Console.WriteLine(i); } ///查找元素 ///查找元素爲5,返回值爲索引 array.IndexOf(5); ////查找元素爲5的,返回值爲true/false array.Contains(5); Console.ReadLine();
List<T>
List<T>類是 ArrayList 類的泛型等效類。 該類使用大小可按需動態增長的數組實現 IList<T> 泛型接口。
看看List<T>所繼承的接口
// 摘要: // 表示可經過索引訪問的對象的強類型列表。提供用於對列表進行搜索、排序和操做的方法。 // // 類型參數: // T: // 列表中元素的類型。 [Serializable] [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))] [DebuggerDisplay("Count = {Count}")] public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
T就是列表中元素的類型,下面咱們以string爲例進行說明一下List<T>的基本用法。在測試過程當中發現List<T>與ArrayList的操做基本徹底相似。主要也是它們共同繼承了IList,ICollection,IEnumerable三個接口。
T固然也能夠是自定義的類型,這也是咱們在平常的編程中應用最爲普遍的。首先來定義一個實體
public class Person { public string FirstName { get; set; } public string LastName { get; set; } }
接下來進行定義和初始化操做
List<Person> list = new List<Person>(); list.Add(new Person() { FirstName="aehyok",LastName="Kris"}); list.Add(new Person() { FirstName = "aehyok", LastName = "Leo" });
由於操做和上面的ArrayList基本是徹底同樣的,因此在此就不作過多的介紹了。
Dictionary
一、Dictionary的普通用法
Dictionary<string, string>是一個泛型。它自己有集合的功能有時候能夠把它當作數組。它的結構是這樣的:Dictionary<[key], [value]>,它的特色是存入對象是須要與[key]值一一對應的存入該泛型,經過某一個必定的[key]去找到對應的值。
static void Main(string[] args) { ///聲明一個Dictionary對象 Dictionary<string, string> dictionaryList = new Dictionary<string, string>(); ///向集合中添加元素 dictionaryList.Add("1", "aehyok"); dictionaryList.Add("2", "Kris"); dictionaryList.Add("3", "Leo"); ////一般添加元素的時候都會先判斷此鍵是否已經存在的 if (dictionaryList.ContainsKey("4")) { dictionaryList.Add("4","Niki"); } ////訪問元素 string str=dictionaryList["4"]; ///遍歷key foreach (var key in dictionaryList.Keys) { Console.WriteLine("OutPut Key:{0}", key.ToString()); } ////遍歷value foreach (string value in dictionaryList.Values) { Console.WriteLine("OutPut Value:{0}", value); } ///循環遍歷key和value foreach (var dic in dictionaryList) { Console.WriteLine("OutPut Key:{0},Value:{1}",dic.Key,dic.Value); } //移除鍵值是4的元素 dictionaryList.Remove("4"); }
二、將dictionary<key,value> 的value當成一個數組
Dictionary<string,string[]> stringList=new Dictionary<string,string[]>(); stringList.Add("1",new string[]{"北京","深圳","上海","廣州"}); stringList.Add("2",new string[]{"重慶","武漢","南京"}); Console.WriteLine("OutPut:" + stringList["1"][1]);
三、將Dictionary<key,value> 的value當成一個實體類
public class Person { public string FirstName { get; set; } public string LastName { get; set; } } static void Main(string[] args) { Dictionary<string,Person> personList=new Dictionary<string,Person>(); Person person = null; for (int i = 0; i < 3; i++) { person = new Person(); person.FirstName = "aehyok"; person.LastName = "Kris"; personList.Add(i.ToString(),person); } foreach (var student in personList) { Console.WriteLine("Output : Key {0}, FirstName : {1}, LastName {2}", student.Key, student.Value.FirstName, student.Value.LastName); } }
四、文章篇幅有限,關於dictionary有關擴展方法暫時不在此進行介紹,若有興趣能夠參見大神做品http://www.cnblogs.com/ldp615/archive/2011/01/28/dictionary-extensions.html
總結
其實能夠發現它們大致的基本操做是相似的,也就是他們擁有繼承共同的接口。這裏也只是簡單的介紹了我以爲最多見的幾個集合的使用。