zoukankan      html  css  js  c++  java
  • C#基础知识系列十(集合)

    数据结构

    数据结构就是相互之间存在一种或多种特定关系的数据元素的集合。 程序界有一点很经典的话,程序设计=数据结构+算法。用源代码来体现,数据结构,就是编程。

      

    集合分类

     在上图中可以看到,集合总体上分为线性集合和非线性集合。线性集合按照存储方式又分为直接存储和顺序存储。

      直接存储,是指该类型的集合数据元素可以直接通过下标(即index)来访问,在C#中直接存储的数据结构有三类:Array(包含数组和List<T>)、string、struct。

      直接存储结构的优点是:向数据结构中添加元素是很高效的,直接放在数据末尾的第一个空位上就可以了。

      直接存储结构的缺点是:向集合插入元素将会变得低效,它需要给插入的元素腾出位置并顺序移动后面的元素。

      顺序存储结构,即线性表。线性表可动态的扩大和缩小,它在一片连续的区域中存储数据元素。线性表不能按照索引进行查找,它是通过对地址的引用来搜索元素的,为了找到某个元素,它必须遍历所有元素,直到找到对应的元素为止。所以,线性表的优点是插入和删除数据效率高,缺点是查找的效率相对来说低一点。

      线性表有可以分为队列、栈以及索引群集,在C#中分别表现为:Queue<T>、Stack<T>,索引群集又进一步泛化为字典类型Dictionary<TKey,TValue>和双向链表LinkedList<T>。

    非线性集合自己在实际应用中比较少,而且感觉也比较复杂,所以在此先不做讨论学习。下面我们就来一一的学习一下日常使用比较频繁的集合吧。

    数组

     数组就是包含同一类型的多个元素。

     数组的声明:int[] intArray;

    注意:数组声明时,方括号([])必须跟在类型的后面,而不是变量名的后面。在C#中,将方括号放在变量名后是不合法的语法。

     数组的初始化:

       我们知道数组是引用类型,所以需要给他们分配堆上的内存。

    1、intArray=new int[3];

    2、intArray=new int[]{1,2,3};

    3、int[] intArray={1,2,3}; 

    数组在声明和初始化后,可以使用索引器进行访问,索引器总是以0开头,表示第一个元素。

    多维数组:

      一般可以是一维数组,二维数组、三维数组、多维数组。下面简单的来看一下数组吧

                ///最简单的一维数组
                int[] intArray = { 1, 2, 3 };
                ///二维数组,两行三列(类似X轴和Y轴平面几何)
                int[,] intTwoArray=new int[2,3];
                intTwoArray[0, 0] = 0;
                intTwoArray[0, 1] = 1;
                intTwoArray[0, 2] = 2;
                intTwoArray[1, 0] = 3;
                intTwoArray[1, 1] = 4;
                intTwoArray[1, 2] = 5; 
                ///二维数组,三行四列
                int[,] intTwoArray2 = new int[,] 
                {
                    { 1, 11, 111 }, 
                    { 2, 22, 222 }, 
                    { 3, 33, 333 }, 
                    { 4, 44, 444 } 
                };
                ///三维数组,三行三列
                int[, ,] intThreeArray;
                intThreeArray = new int[,,] 
                { 
                    { {1,1}, {11,11}, {111,111} }, 
                    { {2,2}, {22,22}, {222,222} }, 
                    { {3,3}, {33,33}, {333,333} }, 
                    { {4,4}, {44,44}, {444,444} } 
                 };

    上面简单的介绍说明了一下一维数组、二维数组和三维数组。

    ArrayList

     ArrayList类继承了以下几个接口

        public class ArrayList : IList, ICollection, IEnumerable, ICloneable

    以下来看一下ArrayList的基本操作

                ///定义一个一维数组
                int[] intArray = { 1, 2, 3 };
                ///直接将一维数组转换为ArrayList;
                ArrayList array = new ArrayList(intArray);
                ///Add方法
                array.Add(5);
                ///Insert方法两个参数,索引位置和值
                array.Insert(3, 7);
                ////删除元素
                ///直接删除Array中的指定元素
                array.Remove(3);  
                ////删除索引位置为3的元素
                array.RemoveAt(3);
                ////删除一个范围,从Array中索引为1的元素开始删除三个元素
                array.RemoveRange(1, 3);
                ///Array的遍历
                foreach (int i in intArray)
                {
                    Console.WriteLine(i);
                }
                ///查找元素
                ///查找元素为5,返回值为索引
                array.IndexOf(5);
                ////查找元素为5的,返回值为true/false
                array.Contains(5);
    
                Console.ReadLine();

    List<T>

      List<T>类是  ArrayList 类的泛型等效类。 该类使用大小可按需动态增加的数组实现  IList<T> 泛型接口。 

    看看List<T>所继承的接口

       // 摘要:
        //     表示可通过索引访问的对象的强类型列表。提供用于对列表进行搜索、排序和操作的方法。
        //
        // 类型参数:
        //   T:
        //     列表中元素的类型。
        [Serializable]
        [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
        [DebuggerDisplay("Count = {Count}")]
        public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

    T就是列表中元素的类型,下面我们以string为例进行说明一下List<T>的基本用法。在测试过程中发现List<T>与ArrayList的操作基本完全类似。主要也是它们共同继承了IList,ICollection,IEnumerable三个接口。

    T当然也可以是自定义的类型,这也是我们在日常的编程中应用最为广泛的。首先来定义一个实体

        public class Person
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }

    接下来进行定义和初始化操作

                List<Person> list = new List<Person>();
                list.Add(new Person() { FirstName="aehyok",LastName="Kris"});
                list.Add(new Person() { FirstName = "aehyok", LastName = "Leo" });

    因为操作和上面的ArrayList基本是完全一样的,所以在此就不做过多的介绍了。

    Dictionary

     1、Dictionary的普通用法

    Dictionary<string, string>是一个泛型。它本身有集合的功能有时候可以把它看成数组。它的结构是这样的:Dictionary<[key], [value]>,它的特点是存入对象是需要与[key]值一一对应的存入该泛型,通过某一个一定的[key]去找到对应的值。

            static void Main(string[] args)
            {
                ///声明一个Dictionary对象
                Dictionary<string, string> dictionaryList = new Dictionary<string, string>();
                ///向集合中添加元素
                dictionaryList.Add("1", "aehyok");
                dictionaryList.Add("2", "Kris");
                dictionaryList.Add("3", "Leo");
                ////通常添加元素的时候都会先判断此键是否已经存在的
                if (dictionaryList.ContainsKey("4"))
                {
                    dictionaryList.Add("4","Niki");
                }
                ////访问元素
                string str=dictionaryList["4"];
                ///遍历key
                foreach (var key in dictionaryList.Keys)
                {
                    Console.WriteLine("OutPut Key:{0}", key.ToString());
                }
                ////遍历value
                foreach (string value in dictionaryList.Values)
                {
                    Console.WriteLine("OutPut Value:{0}", value);
                }
                
                ///循环遍历key和value
                foreach (var dic in dictionaryList)
                {
                    Console.WriteLine("OutPut Key:{0},Value:{1}",dic.Key,dic.Value);
                }
    
                //移除键值是4的元素
                dictionaryList.Remove("4");
    
            }

    2、将dictionary<key,value> 的value当成一个数组

                Dictionary<string,string[]> stringList=new Dictionary<string,string[]>();
                stringList.Add("1",new string[]{"北京","深圳","上海","广州"});
                stringList.Add("2",new string[]{"重庆","武汉","南京"});
                Console.WriteLine("OutPut:" + stringList["1"][1]);

    3、将Dictionary<key,value> 的value当成一个实体类

        public class Person
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
         static void Main(string[] args)
            {
                Dictionary<string,Person> personList=new Dictionary<string,Person>();
                Person person = null;
                for (int i = 0; i < 3; i++)
                {
                    person = new Person();
                    person.FirstName = "aehyok";
                    person.LastName = "Kris";
                    personList.Add(i.ToString(),person);
                }
                foreach (var student in personList)
                {
                    Console.WriteLine("Output : Key {0}, FirstName : {1}, LastName {2}", student.Key, student.Value.FirstName, student.Value.LastName);
                }
            }

    4、文章篇幅有限,关于dictionary有关扩展方法暂时不在此进行介绍,如有兴趣可以参见大神作品http://www.cnblogs.com/ldp615/archive/2011/01/28/dictionary-extensions.html

    总结

    其实可以发现它们大体的基本操作是类似的,也就是他们拥有继承共同的接口。这里也只是简单的介绍了我觉得最常见的几个集合的使用。

    本文转载:https://www.cnblogs.com/aehyok/p/3643928.html

  • 相关阅读:
    UVALive 5983 MAGRID DP
    2015暑假训练(UVALive 5983
    poj 1426 Find The Multiple (BFS)
    poj 3126 Prime Path (BFS)
    poj 2251 Dungeon Master 3维bfs(水水)
    poj 3278 catch that cow BFS(基础水)
    poj3083 Children of the Candy Corn BFS&&DFS
    BZOJ1878: [SDOI2009]HH的项链 (离线查询+树状数组)
    洛谷P3178 [HAOI2015]树上操作(dfs序+线段树)
    洛谷P3065 [USACO12DEC]第一!First!(Trie树+拓扑排序)
  • 原文地址:https://www.cnblogs.com/yueyongsheng/p/15157959.html
Copyright © 2011-2022 走看看