C#中6種常用集合類小結(jié)
一.先來(lái)說(shuō)說(shuō)數(shù)組的不足
也可以說(shuō)集合與數(shù)組的區(qū)別
1.數(shù)組是固定大小的,不能伸縮。雖然System.Array.Resize這個(gè)泛型方法可以重置數(shù)組大小,但是該方法是重新創(chuàng)建新設(shè)置大小的數(shù)組,用的是舊數(shù)組的元素初始化。隨后以前的數(shù)組就廢棄!而集合卻是可變長(zhǎng)的
2.數(shù)組要聲明元素的類型,集合類的元素類型卻是object.
3.數(shù)組可讀可寫(xiě)不能聲明只讀數(shù)組。集合類可以提供ReadOnly方法以只讀方式使用集合。
4.數(shù)組要有整數(shù)下標(biāo)才能訪問(wèn)特定的元素,然而很多時(shí)候這樣的下標(biāo)并不是很有用。集合也是數(shù)據(jù)列表卻不使用下標(biāo)訪問(wèn)。很多時(shí)候集合有定制的下標(biāo)類型,對(duì)于隊(duì)列和棧根本就不支持下標(biāo)訪問(wèn)!
二.下面講述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);//單個(gè)添加
foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 })
{
al.Add(number);//集體添加方法一//清清月兒 http://blog.csdn.net/21aspnet/
}
int[] number2 = new int[2] { 11,12 };
al.AddRange(number2);//集體添加方法二
al.Remove(3);//移除值為3的
al.RemoveAt(3);//移除第3個(gè)
ArrayList al2 = new ArrayList(al.GetRange(1, 3));//新ArrayList只取舊ArrayList一部份
Console.WriteLine("遍歷方法一:");
foreach (int i in al)//不要強(qiáng)制轉(zhuǎn)換
{
Console.WriteLine(i);//遍歷方法一
}
Console.WriteLine("遍歷方法二:");
for (int i = 0; i != al2.Count; i++)//數(shù)組是length
{
int number = (int)al2[i];//一定要強(qiáng)制轉(zhuǎn)換
Console.WriteLine(number);//遍歷方法二
}
}
}
}

2.Stack類
棧,后進(jìn)先出。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();//彈出最后一項(xiàng)不刪除//清清月兒 http://blog.csdn.net/21aspnet/
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類
隊(duì)列,先進(jìn)先出。enqueue方法入隊(duì)列,dequeue方法出隊(duì)列。
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();//彈出最后一項(xiàng)不刪除
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類
哈希表,名-值對(duì)。類似于字典(比數(shù)組更強(qiáng)大)。哈希表是經(jīng)過(guò)優(yōu)化的,訪問(wèn)下標(biāo)的對(duì)象先散列過(guò)。如果以任意類型鍵值訪問(wèn)其中元素會(huì)快于其他集合。GetHashCode()方法返回一個(gè)int型數(shù)據(jù),使用這個(gè)鍵的值生成該int型數(shù)據(jù)。哈希表獲取這個(gè)值最后返回一個(gè)索引,表示帶有給定散列的數(shù)據(jù)項(xiàng)在字典中存儲(chǔ)的位置。
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
// Creates and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add("one", "The");
myHT.Add("two", "quick");
myHT.Add("three", "brown");
myHT.Add("four", "fox");
// Displays the Hashtable.//清清月兒 http://blog.csdn.net/21aspnet/
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類
與哈希表類似,區(qū)別在于SortedList中的Key數(shù)組排好序的。
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個(gè)元素用HybridDicionary,當(dāng)元素增加會(huì)自動(dòng)轉(zhuǎn)為HashTable。
System.Collections.Specialized下還有HybridDicionary類,字符串集合。
System.Collections.Specialized下還有其他類大家可以各取所需!
言歸正轉(zhuǎn)主要說(shuō)NameValueCollection,HashTable 和 NameValueCollection很類似但是他們還是有區(qū)別的,HashTable 的KEY是唯一性,而NameValueCollection則不唯一!
using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.Collections.Hashtable ht = new System.Collections.Hashtable();
ht.Add("DdpMDisplaySeq".Trim(), "Display Sequence".Trim());
ht.Add("DdpMNameChi".Trim(), "Name (Chinese)".Trim());
ht.Add("DdpMNameEng".Trim(), "Name (English)".Trim());
ht.Add("Comment".Trim(), "Comment".Trim());
ht.Add("DdpMMarketCode".Trim(), "Market Code".Trim());
foreach (object key in ht.Keys)
{
Console.WriteLine("{0}/{1} {2},{3}", key, ht[key], key.GetHashCode(), ht[key].GetHashCode());
}
Console.WriteLine(" ");//清清月兒 http://blog.csdn.net/21aspnet/
NameValueCollection myCol = new NameValueCollection();
myCol.Add("DdpMDisplaySeq".Trim(), "Display Sequence".Trim());
myCol.Add("DdpMNameChi".Trim(), "Name (Chinese)".Trim());
myCol.Add("DdpMNameChi".Trim(), "Name (English)".Trim());
myCol.Add("Comment".Trim(), "Comment".Trim());
myCol.Add("DdpMMarketCode".Trim(), "Market Code".Trim());
foreach (string key in myCol.Keys)
{
Console.WriteLine("{0}/{1} {2},{3}", key, myCol[key], key.GetHashCode(), myCol[key].GetHashCode());
}
}
}
}

到此這篇關(guān)于C#中6種常用集合類小結(jié)的文章就介紹到這了,更多相關(guān)C#常用集合類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c#中Invoke與BeginInvoke的用法及說(shuō)明
這篇文章主要介紹了c#中Invoke與BeginInvoke的用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
在C#程序中對(duì)MessageBox進(jìn)行定位的方法
這篇文章主要介紹了在C#程序中對(duì)MessageBox進(jìn)行定位的方法,針對(duì)圖形化界面進(jìn)行調(diào)試,需要的朋友可以參考下2015-07-07
c#實(shí)現(xiàn)識(shí)別圖片上的驗(yàn)證碼數(shù)字
這篇文章主要介紹了c#實(shí)現(xiàn)識(shí)別圖片上的驗(yàn)證碼數(shù)字的方法,本文給大家匯總了2種方法,有需要的小伙伴可以參考下。2015-11-11
Unity ScrollView實(shí)現(xiàn)動(dòng)態(tài)列表生成
這篇文章主要為大家詳細(xì)介紹了Unity ScrollView實(shí)現(xiàn)動(dòng)態(tài)列表生成,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C#實(shí)現(xiàn)標(biāo)題閃爍效果的示例代碼
在Windows系統(tǒng)中,當(dāng)程序在后臺(tái)運(yùn)行時(shí),如果某個(gè)窗體的提示信息需要用戶瀏覽,該窗體就會(huì)不停地閃爍,這樣就會(huì)吸引用戶的注意,下面我們就來(lái)看看如何使用C#實(shí)現(xiàn)這一效果吧2024-04-04
c#方法中調(diào)用參數(shù)的值傳遞方式和引用傳遞方式以及ref與out的區(qū)別深入解析
以下是對(duì)c#方法中調(diào)用參數(shù)的值傳遞方式和引用傳遞方式,以及ref與out的區(qū)進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-07-07

