集合学习

来源:互联网 发布:入驻淘宝企业店铺2017 编辑:程序博客网 时间:2024/05/22 12:14

非泛型集合

1、引入时需加入命名空间System,Collections

2、ArrayList——根据需要动态添加的数组

3、Hashtable——用来存储键值对的哈希表

4、Queue——遵循先进先出的队列

5、Stack——遵循后进先出的栈

泛型集合

1、引入时需加入命名空间System,Collections.Generic

2、List<T>——根据需要动态添加的数组

3、Dictionary<TKey,TValue>——用来存储键值对的哈希表

4、Queue<T>——遵循先进先出的队列

5、Stack<T>——遵循后进先出的栈


ArrayList

1、通过添加或删除元素动态改变数组长度;

2、可灵活的插入、删除、访问元素;

3、非强类型,速度比普通数组慢。

using System;using System.Collections;namespace Lesson01 {class MainClass {   public static void Log ( ArrayList arr ) {string str = "当前数组中有"+arr.Count + "个元素:(";for (int i = 0; i < arr.Count; i++) {str += arr [i];if (i < arr.Count - 1) {str += ",";}}str +=")";Console.WriteLine (str);}public static void Main ( string[] args ) {//1.创建对象ArrayList arr = new ArrayList ();Log (arr);//使用Add()方法添加元素,对元素类型无限制arr.Add ("苏利文");arr.Add (23);arr.Add (17.1f);Log (arr);//使用[下标]来获取指定位置的元素,禁止下标越界//Console.WriteLine ("arr[0]="+arr[0]);//获取当前数组中的元素数量int count = arr.Count;//使用Insert()方法向制定下标位置插入元素arr.Insert(1,"麦克");Log (arr);//使用Remove()方法从数组中删除指定元素arr.Remove("麦克");Log (arr);//使用RemoveAt()方法,将指定下标位置元素删除arr.RemoveAt(1);Log (arr);//使用Contains——判断指定元素是否存在当前数组中bool b =arr.Contains ("苏利文");if (b) {Console.WriteLine ("苏利文在数组中");}else{Console.WriteLine ("苏利文不在数组中");}//使用Clear清空整个数组arr.Clear ();Log (arr);} }}


List

1、大多数情况下比ArrayList执行更好,且类型安全;

2、强类型,速度比ArrayList快。

using System;using System.Collections;using System.Collections.Generic;namespace Lesson01 {class MainClass {   public static void Log (List<string> arr ) {string str = "当前数组中有"+arr.Count + "个元素:(";for (int i = 0; i < arr.Count; i++) {str += arr [i];if (i < arr.Count - 1) {str += ",";}}str +=")";Console.WriteLine (str);}public static void Main ( string[] args ) {//1.创建对象List<string> arr = new List<string>();//List<int> arr = new List<int>();存储均为int类型Log (arr);//使用Add()方法添加元素,对元素类型一旦限制了就无法修改arr.Add ("Hello");arr.Add ("World");Log (arr);//使用[下标]来获取指定位置的元素,禁止下标越界arr[0]="好好学习";string str = arr[1];Console.WriteLine (str);//使用Count——获取当前数组中的元素数量int c = arr.Count;//使用Insert()方法向制定下标位置插入元素arr.Insert(1,"苏利文");Log (arr);//使用Remove()方法从数组中删除指定元素arr.Remove("World");Log (arr);//使用RemoveAt()方法,将指定下标位置元素删除arr.RemoveAt(1);Log (arr);//使用Contains——判断指定元素是否存在当前数组中bool b =arr.Contains ("苏利文");//使用Clear清空整个数组arr.Clear ();Log (arr);} }}



下面是看到一位前辈的文章,主要是介绍Stack和Queue,为了方便复制了部分过来,请大家前往原处学习,这里也表达对原作者的感谢,让我受益匪浅。

地址:http://blog.csdn.net/guofengpu/article/details/52092333

Queue——先进先出的集合

Stack——后进先出的集合。

       Queue相当我们去银行柜台排队,大家依次鱼贯而行。Stack像我们家中洗碗,最后洗好的碗叠在最上面,而下次拿的时候是最先拿到最后叠上去的碗。了解了这样场景,就很容易明白Stack和Queue可用在哪里了。

       比如我们为医院作一个排队叫号的系统,那肯定是选择Queue对象处理。如果我们要为出牌或下棋准备一个场景,那肯定是选择Stack,因为通过Stack至少可为用户提供悔棋。

以下代码仅供学习:

using System;  namespace lesson01{ class Program{  public static void Main()  {   System.Collections.Queue q = new System.Collections.Queue();for (int i = 0; i <= 10; i++){q.Enqueue(i);//入队    }System.Console.WriteLine(q.Count);while (q.Count > 0){System.Console.WriteLine(q.Dequeue());//出队System.Collections.Stack s = new System.Collections.Stack();for (int i = 0; i <= 10; i++){s.Push(i);//入栈}System.Console.WriteLine(s.Count);while (s.Count > 0){System.Console.WriteLine(s.Pop());//出栈}}            }}}