集合類-六種常見的集合類

一、先來說說數組的不足(也可以說集合與數組的區別)

1.數組是固定大小的,不能伸縮。雖然System.Array.Resize這個泛型方法可以重置數組大小,但是該方法是重新創建新設置大小的數組,用的是舊數組的元素初始化。隨後以前的數組就廢棄!而集合卻是可變長的。

2.數組要聲明元素的類型,集合類的元素類型卻是object。

3.數組可讀可寫不能聲明只讀數組。集合類可以提供ReadOnly方法以只讀方式使用集合。

4.數組要有整數下標才能訪問特定的元素,然而很多時候這樣的下標並不是很有用。集合也是數據列表卻不使用下標訪問。很多時候集合有定製的下標類型,對於隊列和棧根本就不支持下標訪問!

二、下面講述6種常用集合

1.ArrayList類

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();
            al.Add(100);//單個添加
            foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 })
           {
                al.Add(number);//集體添加方法一
            }
            int[] number2 = new int[2] { 11,12 };
            al.AddRange(number2);//集體添加方法二
            al.Remove(3);//移除值爲3的
            al.RemoveAt(3);//移除第3個
            ArrayList al2 = new ArrayList(al.GetRange(1, 3));

//新ArrayList只取舊ArrayList一部份

Console.WriteLine("遍歷方法一:");
foreach (int i in al)//不要強制轉換
{
Console.WriteLine(i);//遍歷方法一
}

Console.WriteLine("遍歷方法二:");
for (int i = 0; i != al2.Count; i++)//數組是length
{
int number = (int)al2[i];//一定要強制轉換
Console.WriteLine(number);//遍歷方法二
}
}
}
}

2.Stack類

棧,後進先出。push方法入棧,pop方法出棧。

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Stack sk = new Stack();
Stack sk2 = new Stack();
foreach (int i in new int[4] ...{ 1, 2, 3, 4 })
{
sk.Push(i);//填充
sk2.Push(i);
}
foreach (int i in sk)
{
Console.WriteLine(i);//遍歷
}

sk.Pop();
Console.WriteLine("Pop");
foreach (int i in sk)
{
Console.WriteLine(i);
}
sk2.Peek();//返回位於 Stack 頂部的對象但不將其刪除
Console.WriteLine("Peek");
foreach (int i in sk2)
{
Console.WriteLine(i);
}

while (sk2.Count != 0)
{
int i = (int)sk2.Pop();//清空
sk2.Pop();//清空
}
Console.WriteLine("清空");
foreach (int i in sk2)
{
Console.WriteLine(i);
}
}
}
}

3.Queue類

隊列,先進先出。enqueue方法入隊列,dequeue方法出隊列。

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Queue qu = new Queue();
Queue qu2 = new Queue();
foreach (int i in new int[4] ...{ 1, 2, 3, 4 })
{
qu.Enqueue(i);//填充
qu2.Enqueue(i);
}
foreach (int i in qu)
{
Console.WriteLine(i);//遍歷
}

qu.Dequeue();
Console.WriteLine("Dequeue");
foreach (int i in qu)
{
Console.WriteLine(i);
}
qu2.Peek();//彈出最後一項不刪除
Console.WriteLine("Peek");
foreach (int i in qu2)
{
Console.WriteLine(i);
}

while (qu2.Count != 0)
{
int i = (int)qu2.Dequeue();//清空
qu2.Dequeue();//清空
}
Console.WriteLine("清空");
foreach (int i in qu2)
{
Console.WriteLine(i);
}
}
}
}

4.Hashtable類

哈希表,名-值對。類似於字典(比數組更強大)。哈希表是經過優化的,訪問下標的對象先散列過。

如果以任意類型鍵值訪問其中元素會快於其他集合。GetHashCode()方法返回一個int型數據,使

用這個鍵的值生成該int型數據。哈希表獲取這個值最後返回一個索引,表示帶有給定散列的數據項

在字典中存儲的位置。

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{

Hashtable myHT = new Hashtable();
myHT.Add("one", "The");
myHT.Add("two", "quick");
myHT.Add("three", "brown");
myHT.Add("four", "fox");

Console.WriteLine("The Hashtable contains the following:");
PrintKeysAndValues(myHT);
}

public static void PrintKeysAndValues(Hashtable myHT)
{
foreach (string s in myHT.Keys)
Console.WriteLine(s);

Console.WriteLine(" -KEY- -VALUE-");
foreach (DictionaryEntry de in myHT)
Console.WriteLine(" {0}: {1}", de.Key, de.Value);
Console.WriteLine();
}
}
}

5.SortedList類

與哈希表類似,區別在於SortedList中的Key數組排好序的。

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{

SortedList sl = new SortedList();
sl["c"] = 41;
sl["a"] = 42;
sl["d"] = 11;
sl["b"] = 13;

foreach (DictionaryEntry element in sl)
{
string s = (string)element.Key;
int i = (int)element.Value;
Console.WriteLine("{0},{1}",s,i);
}
}
}
}

6.NameValueCollection類

官方給NameValueCollection定義爲特殊集合一類,在System.Collections.Specialized下。

System.Collections.Specialized下還有HybridDicionary類,建議少於10個元素用

HybridDicionary,當元素增加會自動轉爲HashTable。

System.Collections.Specialized下還有HybridDicionary類,字符串集合。

System.Collections.Specialized下還有其他類大家可以各取所需!

言歸正轉主要說NameValueCollection,HashTable 和 NameValueCollection很類似但是他

們還是有區別的,HashTable 的KEY是唯一性,而NameValueCollection則不唯一!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章