文章出處

          從最開始接觸的數組,到非泛型集合類(ArrayList、HashTable、Queue、Stack)、泛型集合類(List<T>、Dictionary<T>、Queue<T>、Stack<T>), 實際運用中,他們各自有自己的應用領取,就好比剛開始學習英語的時候,有很多個單詞都可以翻譯成憎恨的意思,但總是混淆不清楚,實際上每個單詞的使用場合、語氣色彩都不盡相同。根據場景的需要,使用正確的單詞,表達正確的感情。同樣適用于編程。

         數組: 適用于明確長度無需在其中做插入操作的一組相同類型的數據,因為定義數組的時候,就必須申明長度,如果過長,浪費內存,過短后期又會導致數據溢出

         非泛型集合類: 可適用一組不同類型的數據,內部自動完成裝箱、拆箱(Object)操作,適用廣泛,但是損耗內存 

         泛型集合類:相同類型的一組數據,避免了裝箱、拆箱 造成的內存損耗

關于 List 操作,有簡單類型操作

          List<string> strList = new List<string>();
          strList.Add("123");
          strList.Add("456");

          Console.WriteLine(strList[0]);

如果有相同類型 List<string> list2 對象,將兩個對象進行合并,則遍歷、累加即可

            List<string> strList2 = new List<string>()
            {
                "asd","222","sdfds"
            };

            foreach (var item in strList2)
            {
                strList.Add(item);
            }

 

對于List<Model>  操作,亦如此

            List<User> sUsers = new List<User>();

            var newdata = new User
            {
                Name = "sdf",UserId = 1001,Sex = ""
            };
            
            sUsers.Add(newdata);

然后,合并

            List<User> users = new List<User>()
            {
               new User() {Name = "a",UserId = 1001,Sex = ""},
               new User() {Name = "b",UserId = 1001,Sex = ""},
               new User() {Name = "c",UserId = 1001,Sex = ""},
               new User() {Name = "d",UserId = 1001,Sex = ""}
            };

            foreach (var item in users)
            {
                sUsers.Add(item);
            }

 

關于字典集合類Dictionary<T>, 鍵值對格式,申明時定義 Key 和 value 各自的類型,然后賦值、取值、用值

與其相對的HashTable ,同樣也是鍵值對格式,只是無需申明類型,隨存隨取隨用

            Dictionary<int,string> dictionary = new Dictionary<int, string>();
            dictionary.Add(1,"aaa");  //增加元素
            dictionary.Remove(1);     //移除指定元素
            dictionary.Clear();       //移除所有元素
            bool bo = dictionary.ContainsKey(1);  //判斷指定元素
            Hashtable hashtable = new Hashtable();
            hashtable.Add("123","123");  //增加元素
            hashtable.Add(22,324);
            hashtable.Add(1, "aaa");  
            hashtable.Remove(1);     //移除指定元素
            hashtable.Clear();       //移除所有元素
            bool b = hashtable.ContainsKey(1);  //判斷指定元素

可以看出,Hashtable 對象的內部也是鍵值對格式,其類型均為 object ,這樣使用無類型限制,但是內部集成了裝箱、拆箱操作,增大系統內存的消耗

相同點:調用方法相同,均無序,不可使用索引

不同點:一個是泛型,一個非泛型

以上介紹的集合都可以設置重復的數據,為了避免這種重復的數據存儲,.NET提供集合名稱集。這是一個具有不同項目的集合類型。

還有兩種類型的集合,SortedSet和HastSet。

對于HashSet  主要用來對兩個集合求交集、并集、差集等操作,處理包含以上的方法外,還有

            HashSet<int> name1 = new HashSet<int>();
            name1.Add(8);
            name1.Add(3);

            HashSet<int> name2 = new HashSet<int>();
            name2.Add(4);
            name2.Add(5);

            name1.UnionWith(name2);       //求兩集合的并集
            name1.IntersectWith(name2);   //求兩集合的交集
            name1.ExceptWith(name2);      //求兩集合的差集,name1-name2,若無相等,則值為name1 
            name1.SymmetricExceptWith(name2);  //求兩集合的對稱差集,若無相等,則值為name1+name2,若有則去同后的name1+name2

SortedSet按照排序順序存儲數據,調用方法參照HashSet,僅僅是返回的結果中自動排序

 

----市人皆大笑,舉手揶揄之


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()