C#中的6种常见的集合

1.动态数组(ArrayList)

  动态数组(ArrayList)代表了可被单独索引的对象的有序集合。它基本上可以替代一个数组。但是,与数组不同的是,您可以使用索引在指定的位置添加和移除项目,动态数组会自动重新调整它的大小。它也允许在列表中进行动态内存分配、增加、搜索、排序各项。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demos
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            list.Add(10);//单个添加
            foreach (int number in new int[5]{ 1, 2, 3, 4,5 })
            {
                list.Add(number);//循环添加一个数组
            }
            int[] number2 = new int[2]{ 11,12 };
            list.AddRange(number2);//集体添加
            list.Remove(3);//删除值为3的
            list.RemoveAt(3);//移除下标为3的
            ArrayList list2 = new ArrayList(list.GetRange(1, 3));//新ArrayList(list2)只取旧ArrayList(list)中的一部份List<T>.GetRange(Index, Count)从下标为1的开始取三个
            Console.WriteLine("Method One:");
            foreach (int i in list)
           {
                Console.WriteLine(i);//遍历方法一
            }
            Console.WriteLine("Method Two:");
            for (int i = 0; i != list2.Count; i++)//数组是length
            {
                int number = (int)list2[i];//一定要强制转换
                Console.WriteLine(number);//遍历方法二
            }
            Console.ReadKey();
        }
    }
}

2.哈希表(Hashtable)

  Hashtable 类代表了一系列基于键的哈希代码组织起来的键/值对。它使用来访问集合中的元素。当您使用访问元素时,则使用哈希表,而且您可以识别一个有用的键值。哈希表中的每一项都有一个键/值对。键用于访问集合中的项目。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demos
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();


            ht.Add("1", "hello");
            ht.Add("2", "world");
            ht.Add("3", "I");
            ht.Add("4", "Love");
            ht.Add("5", "China");


            if (ht.ContainsValue("China"))//判断 Hashtable 是否包含指定的值。
            {
                Console.WriteLine("I know you love China!But China already has a collection");
            }
            else
            {
                ht.Add("5", "China");
            }
            // 获取键的集合 
            ICollection key = ht.Keys;

            foreach (string k in key)
            {
                Console.WriteLine(k + ": " + ht[k]);
            }
            Console.ReadKey();
        }
    }
}

3.排序列表(SortedList)

  SortedList 类代表了一系列按照键来排序的键/值对,这些键值对可以通过键和索引来访问。排序列表是数组和哈希表的组合。它包含一个可使用键或索引访问各项的列表。如果您使用索引访问各项,则它是一个动态数组(ArrayList),如果您使用键访问各项,则它是一个哈希表(Hashtable)。集合中的各项总是按键值排序

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demos
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedList sl = new SortedList();

            sl.Add("1", "hello");
            sl.Add("2", "world");
            sl.Add("3", "I");
            sl.Add("4", "Love");
            sl.Add("5", "China");

            if (sl.ContainsValue("China"))
            {
                Console.WriteLine("I know you love China!But China already has a collection");
            }
            else
            {
                sl.Add("5", "China");
            }
            foreach (var item in sl.Keys)
            {
                Console.WriteLine(item + ":" + sl[item]);
            }
            int myIndex = 1;
            Console.WriteLine("The key at index {0} is {1}.", myIndex, sl.GetKey(myIndex));//获得下标为1的键的名称
            Console.WriteLine("The value at index {0} is {1}.", myIndex, sl.GetByIndex(myIndex));//获得键为1的值
            // 获取键的集合 
            ICollection key = sl.Keys;
            foreach (string k in key)
            {
                Console.WriteLine(k + ": " + sl[k]);
            }

            Console.ReadKey();
        }
    }
}

4.堆栈Stack

  堆栈(Stack)代表了一个后进先出的对象集合。当您需要对各项进行后进先出的访问时,则使用堆栈。当您在列表中添加一项,称为推入元素,当您从列表中移除一项时,称为弹出元素

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demos
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack st = new Stack();

            st.Push('A');
            st.Push('B');
            st.Push('C');
            st.Push('D');

            Console.WriteLine("Current stack: ");//当前队列,先存后出
            foreach (char value in st)
            {
                Console.Write(value + " ");
            }
            Console.WriteLine();
            st.Push('V');//向 Stack 的顶部添加一个对象。
            st.Push('H');
            Console.WriteLine("The next poppable value in stack: {0}",st.Peek());//返回在 Stack 的顶部的对象,但不移除它。
            Console.WriteLine("Current stack: ");
            foreach (char value in st)
            {
                Console.Write(value + " ");
            }
            Console.WriteLine();
            Console.WriteLine("Removing values ");
            st.Pop();//移除并返回在 Stack 的顶部的对象
            st.Pop();
            st.Pop();

            Console.WriteLine("Current stack: ");
            foreach (char value in st)
            {
                Console.Write(value + " ");
            }
            Console.ReadKey();
        }
    }
}

5.队列Queue

  队列(Queue)代表了一个先进先出的对象集合。当您需要对各项进行先进先出的访问时,则使用队列。当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demos
{
    class Program
    {
        static void Main(string[] args)
        {
            Queue q = new Queue();

            q.Enqueue('A');
            q.Enqueue('B');
            q.Enqueue('C');
            q.Enqueue('D');

            Console.WriteLine("Current queue: ");//队列先进先出
            foreach (char value in q)
                Console.Write(value + " ");
            Console.WriteLine();
            q.Enqueue('V');//向 Queue 的末尾添加一个对象。
            q.Enqueue('H');
            Console.WriteLine("Current queue: ");
            foreach (char value in q)
                Console.Write(value + " ");
            Console.WriteLine();
            Console.WriteLine("Removing some values ");
            char ch = (char)q.Dequeue();//移除并返回在 Queue 的开头的对象。
            Console.WriteLine("The removed value: {0}", ch);
            ch = (char)q.Dequeue();
            Console.WriteLine("The removed value: {0}", ch);
            Console.ReadKey();
           
        }
    }
}

5.点阵列(BitArray)

  BitArray 类管理一个紧凑型的位值数组,它使用布尔值来表示,其中 true 表示位是开启的(1),false 表示位是关闭的(0)。当您需要存储位,但是事先不知道位数时,则使用点阵列。您可以使用整型索引从点阵列集合中访问各项,索引从零开始。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demos
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建两个大小为 8 的点阵列
            BitArray ba1 = new BitArray(8);
            BitArray ba2 = new BitArray(8);
            byte[] a = { 60 };
            byte[] b = { 13 };
            // 把值 60 和 13 存储到点阵列中
            ba1 = new BitArray(a);
            ba2 = new BitArray(b);
            // ba1 的内容
            Console.WriteLine("Bit array ba1: 60");
            for (int i = 0; i < ba1.Count; i++)
            {
                Console.Write("{0, -6} ", ba1[i]);
            }
            Console.WriteLine();

            // ba2 的内容
            Console.WriteLine("Bit array ba2: 13");
            for (int i = 0; i < ba2.Count; i++)
            {
                Console.Write("{0, -6} ", ba2[i]);
            }
            Console.WriteLine();


            BitArray ba3 = new BitArray(8);
            ba3 = ba1.And(ba2);

            // ba3 的内容
            Console.WriteLine("Bit array ba3 after AND operation: 12");
            for (int i = 0; i < ba3.Count; i++)
            {
                Console.Write("{0, -6} ", ba3[i]);
            }
            Console.WriteLine();

            ba3 = ba1.Or(ba2);
            // ba3 的内容
            Console.WriteLine("Bit array ba3 after OR operation: 61");
            for (int i = 0; i < ba3.Count; i++)
            {
                Console.Write("{0, -6} ", ba3[i]);
            }
            Console.WriteLine();

            Console.ReadKey();
        }
    }
}