.NET總結--泛型與泛型集合,應用場景

泛型優勢數組

  1.提升代碼複用性,代碼簡潔直觀安全

  2.直接存儲數據類型免去數據類型之間得隱式轉換多線程

  3.免去拆箱裝箱過程,提升效率函數

  4.數據類型安全,存儲時會驗證是否對應該類型測試

泛型集合ui

  一. ArrayList與Array與List<T>spa

  1.ArrayList屬於自增容器,也就是無需定義其長度可直接使用而Array須要定義其長度pwa

  2.ArrayList包含操做某範圍元素方法而Array只能獲取一個或設置一個元素得值線程

  3.ArrayList能夠輕鬆建立同步版本,而Array須要手動更新code

  4.ArrayList需引用System.Collections方可以使用而Array只需System便可

  5.已知特定類型(Object除外)Array要比ArrayList好用

  6.ArrayList與List<T>類型類似,但後者類型更加安全並且無需拆裝箱操做即可直接使用

  7.ArrayList沒有類型約束而List<T>須要約束類型

  

ArrayList arrayList1 = new ArrayList();
             arrayList1.
             arrayList1.Add("a");
             arrayList1.Add(1);
             arrayList1.Add("b");
             Response.Write(arrayList1[1]);

  

List < Student > students = new List < Student > ();
   Student stu1 = new Student();
   stu1.Name = "陸小鳳";
   stu1.Number = "0801";
   stu1.Score = 20;
   Student stu2 = new Student();
   stu2.Name = "西門吹雪";
   stu2.Number = "0802";
   stu2.Score = 23;
   students.Add(stu1);
   students.Add(stu2);
   Console.WriteLine("集合中的元素個數爲{0}", students.Count);
   foreach (Student stu in students)
   {
    Console.WriteLine("/t{0}/t{1}/t{2}", stu.Name, stu.Number, stu.Score);
   }
   students.Remove(stu1);
   Console.WriteLine("集合中的元素個數爲{0}", students.Count);
   Console.ReadLine();

  

  二.  HashTable 與 Dictionary<key,value>類型

  1.單線程程序推薦Dictionary,讀寫速度快,容量利用更加便利

  2.多線程程序推薦HashTable,能夠單線程寫入,多線程讀取

  3.Dictionary非線程安全,因此在使用時須要lock一下保護當前語句執行完成,並且兩者都實現IDictionary接口,因此他們通常都是鍵值對

  4.Dictionary爲按序插入數據隊列若其中節點被刪除後 順序會被打亂

  5.HashTable 元素屬於 Object類型因此在操做時常常發生裝箱拆箱操做效率低於Dictionary

  深刻解析:

    HashTable

    1.其本質是鍵值對形式存儲,還有一個相似索引的值由HashCode的值相似索引值做爲該數據位置索引

    2.GetHashCode能夠得到儘可能不會重複的值來做爲該數據存儲地址.這就是散列函數GetHashCode的做用

    3.當HashTable唄佔用大半的時候GetHashCode可能會獲得重複地址,這就是哈希衝突

    4.鍵值對在HashTable中的位置是由Position = (HashCode&0X7FFFFFFF)%HashTable.Length 來肯定的

    5..NET中是用探測方法來解決哈希衝突的,當Position+x若是存在則位移至Position+2*x位置,因此HashTable佔用越多計算時間越長存儲速度越慢

    6.HashTable中當達到當前空間72%時會出現自動擴容,例如空間大小是100當位置佔用到73的時候該空間會自動擴容

    Dictionary

    1.Dictionary是一種變種的HashTable主要是解決哈希衝突方式不一樣

  三. 功能對比

    測試代碼屬於摘抄:

    

public class HashTableTest

    {

        static Hashtable _Hashtable;

        static Dictionary<string, object> _Dictionary;

        static void Main()

        {

            Compare(10);

            Compare(10000);

            Compare(5000000);

            Console.ReadLine();

        }

        public static void Compare(int dataCount)

        {

            Console.WriteLine("-------------------------------------------------\n");

            _Hashtable = new Hashtable();

            _Dictionary = new Dictionary<string, object>();

            Stopwatch stopWatch = new Stopwatch();

            //HashTable插入dataCount條數據須要時間

            stopWatch.Start();

            for (int i = 0; i < dataCount; i++)

            {

                _Hashtable.Add("Str" + i.ToString(), "Value");

            }

            stopWatch.Stop();

            Console.WriteLine(" HashTable插入" + dataCount + "條數據須要時間:" + stopWatch.Elapsed);

   

            //Dictionary插入dataCount條數據須要時間

            stopWatch.Reset();

            stopWatch.Start();

            for (int i = 0; i < dataCount; i++)

            {

                _Dictionary.Add("Str" + i.ToString(), "Value");

            }

            stopWatch.Stop();

            Console.WriteLine(" Dictionary插入" + dataCount + "條數據須要時間:" + stopWatch.Elapsed);

   

            //Dictionary插入dataCount條數據須要時間

            stopWatch.Reset();

            int si = 0;

            stopWatch.Start();

            for(int i=0;i<_Hashtable.Count;i++)

            {

                si++;

            }

            stopWatch.Stop();

            Console.WriteLine(" HashTable遍歷時間:" + stopWatch.Elapsed + " ,遍歷採用for方式");

   

            //Dictionary插入dataCount條數據須要時間

            stopWatch.Reset();

            si = 0;

            stopWatch.Start();

            foreach (var s in _Hashtable)

            {

                si++;

            }

            stopWatch.Stop();

            Console.WriteLine(" HashTable遍歷時間:" + stopWatch.Elapsed + " ,遍歷採用foreach方式");

   

            //Dictionary插入dataCount條數據須要時間

            stopWatch.Reset();

            si = 0;

            stopWatch.Start();

            IDictionaryEnumerator _hashEnum = _Hashtable.GetEnumerator();

            while (_hashEnum.MoveNext())

            {

                si++;

            }

            stopWatch.Stop();

            Console.WriteLine(" HashTable遍歷時間:" + stopWatch.Elapsed + " ,遍歷採用HashTable.GetEnumerator()方式");

   

            //Dictionary插入dataCount條數據須要時間

            stopWatch.Reset();

            si = 0;

            stopWatch.Start();

            for(int i=0;i<_Dictionary.Count;i++)

            {

                si++;

            }

            stopWatch.Stop();

            Console.WriteLine(" Dictionary遍歷時間:" + stopWatch.Elapsed + " ,遍歷採用for方式");

   

            //Dictionary插入dataCount條數據須要時間

            stopWatch.Reset();

            si = 0;

            stopWatch.Start();

            foreach (var s in _Dictionary)

            {

                si++;

            }

            stopWatch.Stop();

            Console.WriteLine(" Dictionary遍歷時間:" + stopWatch.Elapsed + " ,遍歷採用foreach方式");

   

            //Dictionary插入dataCount條數據須要時間

            stopWatch.Reset();

            si = 0;

            stopWatch.Start();

            _hashEnum = _Dictionary.GetEnumerator();

            while (_hashEnum.MoveNext())

            {

                si++;

            }

            stopWatch.Stop();

            Console.WriteLine(" Dictionary遍歷時間:" + stopWatch.Elapsed + " ,遍歷採用Dictionary.GetEnumerator()方式");

   

   

            Console.WriteLine("\n-------------------------------------------------");

        }

    }

    綜上測試可知:

    1.大量數據拆入時HashTable要比Dictionary慢不少

    2.for方式遍歷HashTable和Dictionary速度最快

    3.foreach便利Dictionary時候花費時間更短

  三.  Queue和Stack

  1.Queue與Stack區別在於前者存儲於隊列,後者存儲在棧中

  2.Queue屬於先進先出,Stack屬於先進後出 注意堆棧關係

  四.  SortedList

  1.引用System.Collections.SortList命名空間,其內部存儲至關於兩個數組 鍵數組與值數組 兩者相關聯關係,其中能夠經過鍵來查詢值,也能夠根據值來搜索鍵,可是鍵不能爲空,而值能夠

  2.屬於自增集合,能夠根據自身數據大小來增長佔用空間,也能夠經過屬性設置來減小空間

  3.若集合再也不新增數據能夠經過方法來設置該集合佔用大小,注意該集合不會自動減小佔用空間

  4.若輸入鍵未存儲則默認新增該鍵對應值數據,若該鍵對應值已有數據則默認覆蓋原數據

  5.若不肯定該鍵是否存在能夠根據方法返回值斷定是否存在

  泛型集合SortedList<key,value>例子:

  

static void Main(string[] args)  
06.        {  
07.            // 建立一個SortedList對象          
08.            SortedList mySortedList = new SortedList();  
09.            mySortedList.Add("First", "Hello");  
10.            mySortedList.Add("Second", "World");  
11.            mySortedList.Add("Third", "!");  
12.            mySortedList.Add("Four", "{1}quot;);    
13.  
14.            //列舉SortedList的屬性、鍵、值  
15.            Console.WriteLine("MySortedList");  
16.            Console.WriteLine("  Count:    {0}", mySortedList.Count);  
17.            Console.WriteLine("  Capacity: {0}", mySortedList.Capacity);  
18.            Console.WriteLine("  Keys and Values:");  
19.            PrintIndexAndKeysAndValues(mySortedList);  
20. 
21.            #region SortedList得到鍵、值列表  
22.            SortedList mySortedList1 = new SortedList();  
23.            mySortedList1.Add(1.3, "fox");  
24.            mySortedList1.Add(1.4, "jumped");  
25.            mySortedList1.Add(1.5, "over");  
26.            mySortedList1.Add(1.2, "brown");  
27.            mySortedList1.Add(1.1, "quick");  
28.            mySortedList1.Add(1.0, "The");  
29.            mySortedList1.Add(1.6, "the");  
30.            mySortedList1.Add(1.8, "dog");  
31.            mySortedList1.Add(1.7, "lazy");   
32.  
33.            //得到指定索引處的鍵和值  
34.            int myIndex = 3;  
35.            //     獲取 System.Collections.SortedList 對象的指定索引處的鍵  
36.            Console.WriteLine("The key  at index {0} is {1}.", myIndex, mySortedList1.GetKey(myIndex));  
37.            //     獲取 System.Collections.SortedList 對象的指定索引處的值  
38.            Console.WriteLine("The value at index {0} is {1}.", myIndex, mySortedList1.GetByIndex(myIndex));     
39.  
40.            // 得到SortedList中的鍵列表和值列表         
41.            IList myKeyList = mySortedList1.GetKeyList();  
42.            IList myValueList = mySortedList1.GetValueList();  
43.            // Prints the keys in the first column and the values in the second column.             
44.            Console.WriteLine("\t-KEY-\t-VALUE-");  
45.            for (int i = 0; i < mySortedList1.Count; i++)   
46.                Console.WriteLine("\t{0}\t{1}", myKeyList[i], myValueList[i]);  
47. 
48.            #endregion   
49. 
50.            #region 爲SortedList中的元素從新賦值   
51.            // Creates and initializes a new SortedList.     
52.            SortedList mySortedList2 = new SortedList();  
53.            mySortedList2.Add(2, "two");  
54.            mySortedList2.Add(3, "three");   
55.            mySortedList2.Add(1, "one");  
56.            mySortedList2.Add(0, "zero");  
57.            mySortedList2.Add(4, "four");             
58.            // 打印顯示列表的鍵和值           
59.            Console.WriteLine("The SortedList contains the following values:");  
60.            PrintIndexAndKeysAndValues(mySortedList2);        
61.     
62.            // 得到指定鍵的索引          
63.            int myKey = 2;  
64.            Console.WriteLine("The key \"{0}\" is at index {1}.", myKey, mySortedList2.IndexOfKey(myKey));        
65.            // 得到指定值的索引       
66.            String myValue = "three";  
67.            Console.WriteLine("The value \"{0}\" is at index {1}.", myValue, mySortedList2.IndexOfValue(myValue));        
68.            // 從新設置指定索引處的值           
69.            mySortedList2.SetByIndex(3, "III");   // SetByIndex:替換 System.Collections.SortedList 對象中指定索引處的值  
70.            mySortedList2.SetByIndex(4, "IV");         
71.            //打印顯示列表的鍵和值         
72.            Console.WriteLine("After replacing the value at index 3 and index 4,");  
73.            PrintIndexAndKeysAndValues(mySortedList2);        
74.            #endregion  
75.            Console.ReadKey();  
76.        }  
77.  
78.        //打印SortedList中的鍵和值   
79.        public static void PrintIndexAndKeysAndValues(SortedList myList)    
80.        {              
81.            Console.WriteLine("\t-INDEX-\t-KEY-\t-VALUE-");  
82.            for (int i = 0; i < myList.Count; i++)       
83.            {             
84.                Console.WriteLine("\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i));       
85.            }         
86.            Console.WriteLine();       
87.        }  
88.    }
static void Main(string[] args)  
06.        {  
07.            // 建立一個SortedList對象          
08.            SortedList mySortedList = new SortedList();  
09.            mySortedList.Add("First", "Hello");  
10.            mySortedList.Add("Second", "World");  
11.            mySortedList.Add("Third", "!");  
12.            mySortedList.Add("Four", "{1}quot;);    
13.  
14.            //列舉SortedList的屬性、鍵、值  
15.            Console.WriteLine("MySortedList");  
16.            Console.WriteLine("  Count:    {0}", mySortedList.Count);  
17.            Console.WriteLine("  Capacity: {0}", mySortedList.Capacity);  
18.            Console.WriteLine("  Keys and Values:");  
19.            PrintIndexAndKeysAndValues(mySortedList);  
20. 
21.            #region SortedList得到鍵、值列表  
22.            SortedList mySortedList1 = new SortedList();  
23.            mySortedList1.Add(1.3, "fox");  
24.            mySortedList1.Add(1.4, "jumped");  
25.            mySortedList1.Add(1.5, "over");  
26.            mySortedList1.Add(1.2, "brown");  
27.            mySortedList1.Add(1.1, "quick");  
28.            mySortedList1.Add(1.0, "The");  
29.            mySortedList1.Add(1.6, "the");  
30.            mySortedList1.Add(1.8, "dog");  
31.            mySortedList1.Add(1.7, "lazy");   
32.  
33.            //得到指定索引處的鍵和值  
34.            int myIndex = 3;  
35.            //     獲取 System.Collections.SortedList 對象的指定索引處的鍵  
36.            Console.WriteLine("The key  at index {0} is {1}.", myIndex, mySortedList1.GetKey(myIndex));  
37.            //     獲取 System.Collections.SortedList 對象的指定索引處的值  
38.            Console.WriteLine("The value at index {0} is {1}.", myIndex, mySortedList1.GetByIndex(myIndex));     
39.  
40.            // 得到SortedList中的鍵列表和值列表         
41.            IList myKeyList = mySortedList1.GetKeyList();  
42.            IList myValueList = mySortedList1.GetValueList();  
43.            // Prints the keys in the first column and the values in the second column.             
44.            Console.WriteLine("\t-KEY-\t-VALUE-");  
45.            for (int i = 0; i < mySortedList1.Count; i++)   
46.                Console.WriteLine("\t{0}\t{1}", myKeyList[i], myValueList[i]);  
47. 
48.            #endregion   
49. 
50.            #region 爲SortedList中的元素從新賦值   
51.            // Creates and initializes a new SortedList.     
52.            SortedList mySortedList2 = new SortedList();  
53.            mySortedList2.Add(2, "two");  
54.            mySortedList2.Add(3, "three");   
55.            mySortedList2.Add(1, "one");  
56.            mySortedList2.Add(0, "zero");  
57.            mySortedList2.Add(4, "four");             
58.            // 打印顯示列表的鍵和值           
59.            Console.WriteLine("The SortedList contains the following values:");  
60.            PrintIndexAndKeysAndValues(mySortedList2);        
61.     
62.            // 得到指定鍵的索引          
63.            int myKey = 2;  
64.            Console.WriteLine("The key \"{0}\" is at index {1}.", myKey, mySortedList2.IndexOfKey(myKey));        
65.            // 得到指定值的索引       
66.            String myValue = "three";  
67.            Console.WriteLine("The value \"{0}\" is at index {1}.", myValue, mySortedList2.IndexOfValue(myValue));        
68.            // 從新設置指定索引處的值           
69.            mySortedList2.SetByIndex(3, "III");   // SetByIndex:替換 System.Collections.SortedList 對象中指定索引處的值  
70.            mySortedList2.SetByIndex(4, "IV");         
71.            //打印顯示列表的鍵和值         
72.            Console.WriteLine("After replacing the value at index 3 and index 4,");  
73.            PrintIndexAndKeysAndValues(mySortedList2);        
74.            #endregion  
75.            Console.ReadKey();  
76.        }  
77.  
78.        //打印SortedList中的鍵和值   
79.        public static void PrintIndexAndKeysAndValues(SortedList myList)    
80.        {              
81.            Console.WriteLine("\t-INDEX-\t-KEY-\t-VALUE-");  
82.            for (int i = 0; i < myList.Count; i++)       
83.            {             
84.                Console.WriteLine("\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i));       
85.            }         
86.            Console.WriteLine();       
87.        }  
88.    }
相關文章
相關標籤/搜索