最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#中6種常用集合類(lèi)小結(jié)

 更新時(shí)間:2024年11月08日 10:24:59   作者:技術(shù)拾荒者  
這篇文章主要為大家詳細(xì)介紹了C#中6種常用集合類(lèi),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

一.先來(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ù)組要聲明元素的類(lèi)型,集合類(lèi)的元素類(lèi)型卻是object.

3.數(shù)組可讀可寫(xiě)不能聲明只讀數(shù)組。集合類(lèi)可以提供ReadOnly方法以只讀方式使用集合。

4.數(shù)組要有整數(shù)下標(biāo)才能訪問(wèn)特定的元素,然而很多時(shí)候這樣的下標(biāo)并不是很有用。集合也是數(shù)據(jù)列表卻不使用下標(biāo)訪問(wèn)。很多時(shí)候集合有定制的下標(biāo)類(lèi)型,對(duì)于隊(duì)列和棧根本就不支持下標(biāo)訪問(wèn)!

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

1.ArrayList類(lèi)

 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類(lèi)

棧,后進(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類(lèi)

隊(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類(lèi)

哈希表,名-值對(duì)。類(lèi)似于字典(比數(shù)組更強(qiáng)大)。哈希表是經(jīng)過(guò)優(yōu)化的,訪問(wèn)下標(biāo)的對(duì)象先散列過(guò)。如果以任意類(lèi)型鍵值訪問(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類(lèi)

與哈希表類(lèi)似,區(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類(lèi)

官方給NameValueCollection定義為特殊集合一類(lèi),在System.Collections.Specialized下。

System.Collections.Specialized下還有HybridDicionary類(lèi),建議少于10個(gè)元素用HybridDicionary,當(dāng)元素增加會(huì)自動(dòng)轉(zhuǎn)為HashTable。

System.Collections.Specialized下還有HybridDicionary類(lèi),字符串集合。

System.Collections.Specialized下還有其他類(lèi)大家可以各取所需!

言歸正轉(zhuǎn)主要說(shuō)NameValueCollection,HashTable 和 NameValueCollection很類(lèi)似但是他們還是有區(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種常用集合類(lèi)小結(jié)的文章就介紹到這了,更多相關(guān)C#常用集合類(lèi)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C# 繪制實(shí)時(shí)折線圖,波形圖

    C# 繪制實(shí)時(shí)折線圖,波形圖

    這篇文章主要介紹了C# 繪制實(shí)時(shí)折線圖,波形圖的方法,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C#多線程傳遞參數(shù)及任務(wù)用法示例

    C#多線程傳遞參數(shù)及任務(wù)用法示例

    這篇文章主要介紹了C#多線程傳遞參數(shù)及任務(wù)用法,結(jié)合簡(jiǎn)單實(shí)例形式分析了C#多線程的使用及相關(guān)的參數(shù)傳遞與任務(wù)創(chuàng)建等使用技巧,需要的朋友可以參考下
    2016-06-06
  • Unity 2017使用UGUI實(shí)現(xiàn)大轉(zhuǎn)盤(pán)抽獎(jiǎng)

    Unity 2017使用UGUI實(shí)現(xiàn)大轉(zhuǎn)盤(pán)抽獎(jiǎng)

    這篇文章主要為大家詳細(xì)介紹了Unity 2017使用UGUI實(shí)現(xiàn)大轉(zhuǎn)盤(pán)抽獎(jiǎng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • C#模式畫(huà)刷HatchBrush用法實(shí)例

    C#模式畫(huà)刷HatchBrush用法實(shí)例

    這篇文章主要介紹了C#模式畫(huà)刷HatchBrush用法,實(shí)例分析了模式畫(huà)刷HatchBrush繪圖的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • 使用C#代碼統(tǒng)計(jì)Word文檔的單詞、字符、段落、行數(shù)和頁(yè)數(shù)

    使用C#代碼統(tǒng)計(jì)Word文檔的單詞、字符、段落、行數(shù)和頁(yè)數(shù)

    對(duì)單詞、字符、段落、行數(shù)和頁(yè)數(shù)進(jìn)行準(zhǔn)確統(tǒng)計(jì),對(duì)于實(shí)現(xiàn)精確的文檔分析至關(guān)重要,通過(guò)細(xì)致地追蹤這些指標(biāo),作者可以更深入地了解文檔的長(zhǎng)度、結(jié)構(gòu)和整體構(gòu)成,在本文中,我們將介紹如何使用 C# 代碼統(tǒng)計(jì) Word 文檔中的單詞、字符、段落、行數(shù)和頁(yè)數(shù),需要的朋友可以參考下
    2025-12-12
  • unity繪制一條流動(dòng)的弧線(貝塞爾線)

    unity繪制一條流動(dòng)的弧線(貝塞爾線)

    這篇文章主要為大家詳細(xì)介紹了unity繪制一條流動(dòng)弧線的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • C# 文件操作函數(shù) 創(chuàng)建文件 判斷存在

    C# 文件操作函數(shù) 創(chuàng)建文件 判斷存在

    本文列舉了C#中文件操作中常用的函數(shù),創(chuàng)建文件和判斷文件存不存在的基本使用,簡(jiǎn)單實(shí)用,希望能幫到大家。
    2016-05-05
  • C#定時(shí)器Timer實(shí)現(xiàn)精確到1-2毫秒以內(nèi)

    C#定時(shí)器Timer實(shí)現(xiàn)精確到1-2毫秒以內(nèi)

    最近在排查項(xiàng)目OTA的一個(gè)問(wèn)題,觸發(fā)了一毫秒或者2毫秒執(zhí)行一次進(jìn)程間通信的,導(dǎo)致通信阻塞的問(wèn)題,這樣就需要用到模擬觸發(fā)1ms或者2ms觸發(fā)事件,所以本文給大家介紹了C#?定時(shí)器?Timer?如何精確到?1-2?毫秒以內(nèi),需要的朋友可以參考下
    2024-12-12
  • C#簡(jiǎn)單了解接口(Interface)使用方法

    C#簡(jiǎn)單了解接口(Interface)使用方法

    這篇文章主要介紹了C#簡(jiǎn)單了解接口(Interface)使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 基于C# winform實(shí)現(xiàn)圖片上傳功能的方法

    基于C# winform實(shí)現(xiàn)圖片上傳功能的方法

    這篇文章主要介紹了基于C# winform實(shí)現(xiàn)圖片上傳功能的方法,很實(shí)用的功能,需要的朋友可以參考下
    2014-07-07

最新評(píng)論

宿迁市| 花莲市| 报价| 锡林郭勒盟| 柳河县| 炉霍县| 吉首市| 基隆市| 鞍山市| 武汉市| 垫江县| 景洪市| 土默特右旗| 华亭县| 南靖县| 广元市| 汽车| 万盛区| 同仁县| 兰坪| 朝阳区| 延长县| 洱源县| 浦江县| 广饶县| 通河县| 济源市| 西华县| 富源县| 夏邑县| 襄汾县| 光山县| 古浪县| 康定县| 扎囊县| 广汉市| 行唐县| 诸城市| 焉耆| 龙州县| 吉首市|