.net基础---集合
集合的类型:
1.ArrayList:大小可变的动态数组
2.BitArray:管理位值的压缩数组,该值表示为布尔值,其中true表示位是打开的(1),false表示位是关闭的(0)
3.Hashtable:集合中每个元素都有一个标示,称为"键"(key)
4.Query:队列,队列中的元素先进先出
5.SortedList:表示键值对的集合,这些键值对可以排序,可以通过键和索引访问
6.Stack:堆栈,堆栈的元素后进先出
详细介绍:
1.ArrayList:与数组类似,不同的是其大小可变,而且其元素类型为object,因而他是一个非常好的"多态数组"
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 ArrayList al = new ArrayList(); 6 al.Add(100);//增加整数 7 al.Add(0.1);//增加浮点数 8 al.Add("Hello");//增加字符串 9 al.Add(new ArrayList());//增加对象 10 11 //删除元素 12 al.RemoveAt(3);//删除第4个元素 13 //访问元素 14 int result = Convert.ToInt32(al[0]);//取出第一个整数 15 string str = al[2].ToString();//取出第3个字符串 16 } 17 }
ArrayList当元素放入和取出的时候进行了装箱和拆箱的操作,会影响运行效率,所以尽量不要使用ArrayList,用ArrayList<object>就可以避免装箱拆箱提高效率
ArrayList允许null和重复元素
2.Hashtable:Hashtable中每个元素都有独一无二的键(key),通过key来访问其中的元素
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Hashtable hs = new Hashtable(); 6 hs.Add("One",new object()); 7 hs.Add("Two",100); 8 hs.Add("Three","3"); 9 10 hs.Remove("Three"); 11 12 object obj = hs["One"]; 13 int a = Convert.ToInt32(hs["Two"]); 14 } 15 }
和ArrayList类似Hashtable取元素也要进行装箱和拆箱,可以使用泛型Hashtable<object key,object value>来优化他
Hashtable键是不允许重复的,编译的时候不会出错,但是运行的时候会有问题
Hashtable键不能为null,但是值可以
3.Queue:队列中的项只能从先进去的开始删除,不能无视规则的删除队列中的项,比如不能直接删除第二项,或者最后一项,要删除第二项只能先删除第一项,然后才能删除第二项,当前的第一项(先进先出)
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 //1.添加元素 6 Console.WriteLine("1.添加5个元素"); 7 Queue<string> numbers = new Queue<string>(); 8 numbers.Enqueue("one"); 9 numbers.Enqueue("two"); 10 numbers.Enqueue("three"); 11 numbers.Enqueue("four"); 12 numbers.Enqueue("five"); 13 //2.遍历元素 14 Console.WriteLine("2.遍历元素"); 15 foreach (string s in numbers) 16 Console.WriteLine(s); 17 //3.使用Dequeue方法移除列头对象并返回该对象,Peek方法返回列头对象但是不移除 18 Console.WriteLine("3.使用Dequeue方法移除列头对象并返回该对象,Peek方法返回列头对象但是不移除"); 19 Console.WriteLine("Dequeue:{0}",numbers.Dequeue()); 20 Console.WriteLine("Peek:{0}",numbers.Peek()); 21 //4.使用ToArray方法创建一个数组对象并将队列中的元素赋值到数组中 22 string[] strArray = numbers.ToArray(); 23 Console.WriteLine("4.使用ToArray方法创建一个数组对象并将队列中的元素赋值到数组中"); 24 foreach (string s in strArray) 25 Console.WriteLine(s); 26 //5.由数组创建Queue 27 Queue queueCopy = new Queue(strArray); 28 Console.WriteLine("5.由数组创建的Queue对象"); 29 foreach (string s in queueCopy) 30 Console.WriteLine(s); 31 //6.创建一个大小是Queue 2倍的数组,使用copyto方法把queue中的对象复制到数组中 32 string[] array2=new string[numbers.Count * 2]; 33 numbers.CopyTo(array2, 0);//从索引0开始,讲全部内容复制到数组中 34 Console.WriteLine("6.创建一个大小是Queue 2倍的数组,使用copyto方法把queue中的对象复制到数组中"); 35 for (int i = 0; i < numbers.Count; i++) 36 Console.WriteLine(array2[i].ToString()); 37 //7.使用contains方法显示字符串"four"是否存在于队列中,用clear清空队列 38 Console.WriteLine("7.使用contains方法显示字符串'four'是否存在于队列中,用clear清空队列"); 39 Console.WriteLine("four是否存在于队列:{0}",numbers.Contains("four")); 40 Console.WriteLine("清空队列"); 41 numbers.Clear(); 42 Console.WriteLine("队列长度:{0}",numbers.Count); 43 Console.ReadKey(); 44 } 45 }
Queue在接收顺序存储消息方面非常有用,对消息进行顺序处理
Queue允许null和重复元素
4.SortedList:和Hashtable类似,不过他不单单可以通过key来取值,也可以通过索引来取key的位置以及该索引对应的value,而且加入其中的元素会根据key来排序
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 7 // 创建一个SortedList对象 8 SortedList mySL = new SortedList(); 9 mySL.Add("First", "Hello"); 10 mySL.Add("Second", "World"); 11 mySL.Add("Third", "!"); 12 //列举SortedList的属性、键、值 13 Console.WriteLine("mySL"); 14 Console.WriteLine(" Count: {0}", mySL.Count); 15 Console.WriteLine(" Capacity: {0}", mySL.Capacity); 16 Console.WriteLine(" Keys and Values:"); 17 PrintIndexAndKeysAndValues(mySL); 18 19 #region SortedList获得键、值列表 20 SortedList mySL1 = new SortedList(); 21 mySL1.Add(1.3, "fox"); 22 mySL1.Add(1.4, "jumped"); 23 mySL1.Add(1.5, "over"); 24 mySL1.Add(1.2, "brown"); 25 mySL1.Add(1.1, "quick"); 26 mySL1.Add(1.0, "The"); 27 mySL1.Add(1.6, "the"); 28 mySL1.Add(1.8, "dog"); 29 mySL1.Add(1.7, "lazy"); 30 31 //获得指定索引处的键和值 32 int myIndex = 3; 33 Console.WriteLine("The key at index {0} is {1}.", myIndex, mySL1.GetKey(myIndex)); 34 Console.WriteLine("The value at index {0} is {1}.", myIndex, mySL1.GetByIndex(myIndex)); 35 36 // 获得SortedList中的键列表和值列表 37 IList myKeyList = mySL1.GetKeyList(); 38 IList myValueList = mySL1.GetValueList(); 39 40 // Prints the keys in the first column and the values in the second column. 41 Console.WriteLine("\t-KEY-\t-VALUE-"); 42 for (int i = 0; i < mySL1.Count; i++) 43 Console.WriteLine("\t{0}\t{1}", myKeyList[i], myValueList[i]); 44 45 #endregion 46 47 #region 为SortedList中的元素重新赋值 48 // Creates and initializes a new SortedList. 49 SortedList mySL2 = new SortedList(); 50 mySL2.Add(2, "two"); 51 mySL2.Add(3, "three"); 52 mySL2.Add(1, "one"); 53 mySL2.Add(0, "zero"); 54 mySL2.Add(4, "four"); 55 56 // 打印显示列表的键和值 57 Console.WriteLine("The SortedList contains the following values:"); 58 PrintIndexAndKeysAndValues(mySL2); 59 60 // 获得指定键的索引 61 int myKey = 2; 62 Console.WriteLine("The key \"{0}\" is at index {1}.", myKey, mySL2.IndexOfKey(myKey)); 63 64 // 获得指定值的索引 65 String myValue = "three"; 66 Console.WriteLine("The value \"{0}\" is at index {1}.", myValue, mySL2.IndexOfValue(myValue)); 67 68 69 // 重新设置指定索引处的值 70 mySL2.SetByIndex(3, "III"); 71 mySL2.SetByIndex(4, "IV"); 72 73 //打印显示列表的键和值 74 Console.WriteLine("After replacing the value at index 3 and index 4,"); 75 PrintIndexAndKeysAndValues(mySL2); 76 #endregion 77 78 Console.ReadKey(); 79 80 } 81 public static void PrintIndexAndKeysAndValues(SortedList myList) 82 { 83 Console.WriteLine("\t-INDEX-\t-KEY-\t-VALUE-"); 84 for (int i = 0; i < myList.Count; i++) 85 { 86 Console.WriteLine("\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i)); 87 } 88 Console.WriteLine(); 89 } 90 }
5.stack:最大的特点就是"后进先出",即先放入堆栈的数据最后取出,是一种后来者居上的数据存取形式
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 //压入数据 6 Console.WriteLine("1.压入数据"); 7 Stack stk = new Stack(); 8 stk.Push(1); 9 stk.Push(2); 10 stk.Push(3); 11 foreach (object obj in stk) 12 Console.WriteLine(obj); 13 //读取第一个数据 14 Console.WriteLine("2.读取第一个数据"); 15 Console.WriteLine(stk.Peek()); 16 //取出并移除数据 17 Console.WriteLine("3.取出并移除数据"); 18 while (stk.Count > 0) 19 Console.WriteLine(stk.Pop()); 20 Console.WriteLine("数据个数:{0}",stk.Count); 21 Console.ReadKey(); 22 }
访问集合的通用方法
虽然各种集合都有自己访问元素的方式,但在.NET中所有集合对象都实现了IEnumerable接口,因此,我们可以写出一下的通用集合元素访问代码
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 int[] values = new int[] { 1,2,3}; 6 ArrayList al = new ArrayList(); 7 al.Add("one"); 8 al.Add("two"); 9 al.Add("three"); 10 Hashtable hs = new Hashtable(); 11 hs.Add(1, "hs_1"); 12 hs.Add(2, "hs_2"); 13 hs.Add(3, "hs_3"); 14 Queue queue = new Queue(); 15 queue.Enqueue("qu_1"); 16 queue.Enqueue("qu_2"); 17 queue.Enqueue("qu_3"); 18 SortedList sl = new SortedList(); 19 sl.Add(1, "sl_1"); 20 sl.Add(2, "sl_2"); 21 sl.Add(3, "sl_3"); 22 Stack stk = new Stack(); 23 stk.Push("stk_1"); 24 stk.Push("stk_2"); 25 stk.Push("stk_3"); 26 PrintCollection(values); 27 PrintCollection(hs); 28 PrintCollection(queue); 29 PrintCollection(sl); 30 PrintCollection(stk); 31 Console.ReadKey(); 32 } 33 public static void PrintCollection(IEnumerable obj) 34 { 35 //获取迭代访问器 36 IEnumerator itor = obj.GetEnumerator(); 37 //只有集合中还有未被访问的元素,MoveNext方法就返回true 38 while (itor.MoveNext()) 39 { 40 //itor.Current返回当前对象 41 //例子只做了对DictionaryEntity的简单处理 42 if (itor.Current is DictionaryEntry) 43 { 44 DictionaryEntry de = (DictionaryEntry)itor.Current; 45 Console.WriteLine(de.Value.ToString()); 46 } 47 else 48 { 49 Console.WriteLine(itor.Current.ToString()); 50 } 51 } 52 }
IEnumerable接口只有一个成员,就是GetEnumerator()方法,他向外界返回一个实现了IEnumerator接口的对象,此对象称为"枚举数对象"
枚举数对象拥有IEnumerable接口规定的一下方法和属性
1.Current属性:代表正在访问的元素
2.MoveNext()方法:将枚举数推进到集合的下个元素
3.Reset()方法:将枚举数设置为初始位置,该位置位于集合中的第一个元素之前
虽然使用通用方法比较灵活,但是取出来的元素都要经过装箱拆箱,所以可以用for each循环语句来替代
浙公网安备 33010602011771号