zoukankan      html  css  js  c++  java
  • 泛型List、HashTable

              从最开始接触的数组,到非泛型集合类(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,仅仅是返回的结果中自动排序

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

  • 相关阅读:
    React开发入门
    API爬虫--Twitter实战
    网页爬虫--scrapy入门
    爬虫入门(实用向)
    随谈10年的技术生涯和技术成长
    html元素的显示和隐藏
    Ubuntu下用cue文件对ape和wav文件自动分轨
    Bash内置命令exec和重定向
    Bash提示符
    Bash启动选项
  • 原文地址:https://www.cnblogs.com/Sientuo/p/7267646.html
Copyright © 2011-2022 走看看