zoukankan      html  css  js  c++  java
  • C#常用的几种集合和使用范围

    C#中常用的集合主要分为两类:泛型集合和非泛型集合

    使用非泛型集合需要引入命名空间System.Collection
    ArrayList----可以根据需要动态增加的数组
    Hashtable----用来存储键值对的哈希表
    Queue----循环先进先出的队列
    Stack-----遵循后进先出的栈
     
    使用泛型集合需要引入命名空间System.Collection.Generic
    List<T>--可以根据需要动态增加的数组
    Dictionary<Tkey,TValue>---用来存储键值对的哈希表
    Queue<T>----遵循先进先出的队列
    Stack<T>----遵循后进先出的栈
     
    1.1:ArrayList:其主要应用在当需要进行存储的数据中有多种类型,同时程序也不是太计较性能的得失,因为其要进行装箱和拆箱操作,这个操作非常消耗性能
    装箱:就是将值类型的数据打包到引用类型的实例中
    拆箱:就是从引用数据中提取值类型
    其中的方法代码如下
                //1.首先创建对象
                ArrayList arr=new ArrayList();
                //使用Add()方法添加元素,对元素类型没有限制
                arr.Add (12);
                arr.Add ("1234");
                arr.Add (12.7f);
                //使用  /下标/  来获取指定位置的元素
                Console.WriteLine ("arr[0]="+arr[0]);
                //获取当前数组的数量
                int count=arr.Count;
                //使用insert()方法向指定下标位置插入元素
                arr.Insert(1,"老张");
                Console.WriteLine ("arr[1]="+arr[1]);
                //使用Remove()方法从数组中删除指定元素
                arr.Remove("老张");
                Console.WriteLine ("arr[1]="+arr[1]);
                //使用RemoveAt()方法,将指定下标位置的元素删除
                arr.RemoveAt(0);
                Log (arr);
                //判断指定元素中是否存在当前数组中
                bool b=arr.Contains("老王");
                if (b)
                {
                    Console.WriteLine ("老王在数组中");
                }
                else
                {
                    Console.WriteLine ("老王不在数组中!!!!");
                }

    1.2:List:设计的程序类型相对要较安全,并且性能要相对较高

                //1.首先创建对象
                List<string> list=new List<string>();
                //使用Add()方法添加元素,只能添加字符串类型的元素
                list.Add("123");
                list.Add ("778");
    
                //实用[下标]来获取指定位置的元素
                Console.WriteLine ("arr[0]="+arr[0]);
                //获取当前数组中的元素的数目
                int count1=arr.Count;
                //使用insert()方法向指定下标位置插入元素
                arr.Insert(1,"老张");
                Console.WriteLine ("arr[1]="+arr[1]);
                //使用Remove()方法从数组中删除指定元素
                arr.Remove("老张");
                //使用RemoveAr()方法删除指定下标位置的元素
                list.RemoveAt(0);
                Console.WriteLine (list[0]);
                //Contains()判断指定元素是否存在在当前数组中
                bool b1=list.Contains("老王");
                if (b1)
                {
                    Console.WriteLine ("老王在数组中!!!!");
                }
                else
                {
                    Console.WriteLine ("老王不在数组中!!!!");
                }
                //进行集合的清空
                list.Clear ();
                Console.WriteLine (list.Count);

    1.3:字典对象(Dictionary):一般作为聚合统计或者快速使用特征访问

                //Dictionary是存储键和值的集合
                //Dictionary是无序的,键Key是唯一的
                Console.WriteLine ();
                Console.WriteLine ("Dictionary字典对象的内容:");
                //创建一个字典对象,key的类型是string,Value的类型是int类型
                Dictionary<string,int> dic = new Dictionary<string, int> ();
                //Add方法用来添加键值对
                dic.Add("laowang",12);
                dic.Add ("laozhang",12);
    
                //从字典中移除键值对
                //dic.Remove("laowang");
                //清空当前字典
                //dic.Clear();
                //获取当前字典中的keyValue的个数
                int count2=dic.Count;
                Console.WriteLine ("当前字典中有"+count2+"个KeyValue");
                //通过Key获取Value
                int age=dic["laowang"];
                Console.WriteLine (age);
    
                //检查字典中是否包含指定的Key
                bool b2=dic.ContainsKey("xiaowang");
                Console.WriteLine (b2);
                //检查字典中是否包含指定的Value
                bool b3=dic.ContainsValue(12);
                Console.WriteLine (b3);
    
                //尝试获取指定的key所对应的Value
                //如果当前字典中包含laowang这个key,那么就获取对应的Value并保存在s里,bb=true
                //如果当前字典中不包含laowang这个key,那么s=0,bb=false;
                int s=0;
                bool bb = dic.TryGetValue ("laowang",out s);
                Console.WriteLine (bb);
                Console.WriteLine (s);                

    1.4:栈:一般栈用在DFS(深度优先遍历)大家可以查一下这个算法不懂得

                //栈和队列根据需要的容量自动增长
                //栈和队列都允许重复元素
                Console.WriteLine ("栈对象的内容:");
                Stack<string> ss=new Stack<string>();
                int count4=ss.Count;
                ss.Clear ();
                bool b4=ss.Contains("老王");
                Console.WriteLine (b4);
                //Push把元素入栈
                ss.Push("老王");
                ss.Push ("小张");
                ss.Push ("小明");
                //获取并移除栈中的元素
                //Pop把元素出栈,栈中就没有这个元素了
                //Pop把元素出栈,其按照的规则是后进先出的原则
                string s1=ss.Pop();
                Console.WriteLine (s1);
                string s2 = ss.Pop ();
                Console.WriteLine (s2);
                string s3 = ss.Pop ();
                Console.WriteLine (s3);
                //无法再次输出了因为栈中已经没有元素了
    //            string s4 = ss.Pop ();
    //            Console.WriteLine (s4);

    1.5:队列:一般用在BFS(广度优先遍历)

                Console.WriteLine ("队列内容:");
                Queue<string> q = new Queue<string> ();
                q.Clear ();
                int count6=q.Count;
                bool b7=q.Contains("老王");
                //向队列中添加元素
                q.Enqueue("老王");
                q.Enqueue ("老张");
                q.Enqueue ("小明");
                //获取队列中的元素
                string ss1=q.Dequeue();
                Console.WriteLine (ss1);
                string ss2=q.Dequeue();
                Console.WriteLine (ss2);
                string ss3=q.Dequeue();
                Console.WriteLine (ss3);

    以上就是我为大家总结的一些小知识,希望能帮助到大家,如果有错误的可以写在评论里,大家一起讨论!!!!!!!

  • 相关阅读:
    .NET Interop 工具集
    关于正弦波的算法
    Windows Phone 系列 本地数据存储
    Xaml cannot create an instance of “X”
    Windows Phone 系列 使用 MVVM绑定时无法获取当前值
    Windows Phone 系列 应用程序图标无法显示
    Windows Phone 系列 WPConnect无法上网的问题
    Windows Phone 系列 使用 Windows Phone 保存铃声任务
    WP7.5提交应用
    Windows Phone 系列 动态删除ObservableCollection
  • 原文地址:https://www.cnblogs.com/baosong/p/8460372.html
Copyright © 2011-2022 走看看