1. 程式人生 > >C#中的6種常見的集合

C#中的6種常見的集合

行動 元素 rem 大小 for 轉換 contain demo 索引

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();
        }
    }
}

C#中的6種常見的集合