本文对常用的数据结构:Array, ArrayList,List,IList,ICollection, Stack, Queue, HashTable, Dictionary, IQueryable, IEnumerable等进行详述。

一、Collection(集合) Collection是数据记录集合, 编写代码过程中,常常需要合适的容器保存临时数据,方便修改和查找,如何选取合适的数据容器,关键在于将执行的数据操作以及数据记录是否大量。

二、Array(数组)

特征

1. 固定大小,数组的大小是初始化时决定无法修改的数值。

2. 强类型,存储数据元素类型必须在初始化时指定,因此在运行时,不需要耗费额外的时间来定义数组类型,能够大大提升运行效率。

3. 可使用Foreach关键字实现数组迭代和查找。

因为数组大小是固定的,且是强类型数据结构,因此在运行时只占用很少的内存,运行时效率很高。

三、ArrayList

1. ArrayList 没有固定的长度,容量可动态增加,可应用于开发人员无法确定数组元素个数等场景,当然这种情况下,在定义结构体的时候会非常耗时。

2. ArrayList 不是强类型,ArrayList中不同元素类型可以不相同,并且需要在运行时根据实际的输入来确定元素类型。因此在运行时消耗内存较多。

3. 可使用Froeach 关键字操作ArrayList。

ArrayList支持String,int,以及十进制小数类型。

四、HashTable(哈希表)

HashTable是一种定义关键字的数据结构体,使用哈希表查找数据非常方便,哈希表既不是强类型也不固定大小限制。

五、Stack

栈是最典型的数据结构,栈具有优先级划分的数据结构,栈为每个内容项定义优先级,表示每个Item入栈和出栈的优先顺序。因此操作栈中的数据,需要先将数据push 到栈的顶部,需要删除元素必须变成栈顶部,即要遵守后进先出(LIFO)的原则。

栈与哈希表一样既不是强类型也不限制元素个数。

Push 操作

  1. //Stack is LIFO: Last in First Out
  2. System.Collections.Stack objStackPush = new System.Collections.Stack();
  3. //By Push method you can insert item at the top of the stack
  4. objStackPush.Push("Mahsa");
  5. objStackPush.Push("Hassankashi");
  6. this.lblPop.Text = "";
  7. this.ListBoxStack.DataSource = objStackPush.ToArray();
  8. this.ListBoxStack.DataBind();

复制代码

Pop操作

  1. System.Collections.Stack objStackPop = new System.Collections.Stack();
  2. objStackPop.Push("Mahsa");
  3. objStackPop.Push("Hassankashi");
  4. //By Pop method you can remove item from the top of the stack --> Last in First in
  5. this.lblPop.Text = objStackPop.Pop().ToString();
  6. this.ListBoxStack.DataSource = objStackPop.ToArray();
  7. this.ListBoxStack.DataBind();

复制代码

六、Queue

Queue同栈一样也是具有优先级定义的结构体,遵循的规则是先进先出(FIFO),既不是强类型也不具有固定的大小限制。

入队操作

  1. //Queue is FIFO: First in First Out
  2. System.Collections.Queue objQueue = new System.Collections.Queue();
  3. //By Enqueue method you can insert item at the END of the Queue
  4. objQueue.Enqueue("Mahsa");
  5. objQueue.Enqueue("Hassankashi");
  6. objQueue.Enqueue("Cosmic");
  7. objQueue.Enqueue("Verse");
  8. this.lblQueue.Text = "";
  9. this.ListBoxQueue.DataSource = objQueue.ToArray();
  10. this.ListBoxQueue.DataBind();

复制代码

出队操作

  1. System.Collections.Queue objQueue = new System.Collections.Queue();
  2. objQueue.Enqueue("Mahsa");
  3. objQueue.Enqueue("Hassankashi");
  4. objQueue.Enqueue("Cosmic");
  5. objQueue.Enqueue("Verse");
  6. //By Dequeue method you can remove item from the BEGINING of the Queue --> First in First out FIFO
  7. this.lblQueue.Text=objQueue.Dequeue().ToString();
  8. this.ListBoxQueue.DataSource = objQueue.ToArray();
  9. this.ListBoxQueue.DataBind();

复制代码

入队操作

  1. System.Collections.Queue objQueue = new System.Collections.Queue();
  2. objQueue.Enqueue("Mahsa");
  3. objQueue.Enqueue("Hassankashi");
  4. objQueue.Enqueue("Cosmic");
  5. objQueue.Enqueue("Verse");
  6. //By Dequeue method you can remove item from the BEGINING of the Queue --> First in First out FIFO
  7. this.lblQueue.Text=objQueue.Dequeue().ToString();
  8. this.ListBoxQueue.DataSource = objQueue.ToArray();
  9. this.ListBoxQueue.DataBind();

复制代码

七、List

什么情况下需要使用List?

1. List长度可不固定

2. 当数据为通用类型,List是强类型,List中元素类型不需要等到运行时来确定,这种特性使得List 运行时效率非常高。

3. 可使用Foreach关键字。

因为List不需要设定固定的大小,List灵活度高,且效率高常用于开发过程中。

  1. //Like Array is Strong Type
  2. //Like ArrayList with No Dimension
  3. System.Collections.Generic.List<string> strList = new List<string>();
  4. strList.Add("Mahsa");
  5. strList.Add("Hassankashi");
  6. strList.Add("Cosmic");
  7. strList.Add("Verse");
  8. this.ListBoxListGeneric.DataSource = strList;
  9. this.ListBoxListGeneric.DataBind();
  10. System.Text.StringBuilder str = new System.Text.StringBuilder();
  11. foreach (var item in strList)
  12. {
  13. str.Append(" , " + item);
  14. }
  15. this.lblList.Text = str.ToString();

复制代码

八、IList

IList 继承了List,包含多种方法的List接口。如果你无法判断代码改动的可能性,可以使用IList接口,减少模块之间的依赖性。IList是接口因此无法被实例化,所以必须使用List来初始化。

  1. //Ilist can not be instantiate from Ilist , so it should be instantiate from List
  2. System.Collections.Generic.IList<string> strIList = new List<string>();
  3. strIList.Add("Mahsa");
  4. strIList.Add("Hassankashi");
  5. strIList.Add("Cosmic");
  6. strIList.Add("Verse");
  7. this.ListBoxListGeneric.DataSource = strIList;
  8. this.ListBoxListGeneric.DataBind();
  9. System.Text.StringBuilder str = new System.Text.StringBuilder();
  10. foreach (var item in strIList)
  11. {
  12. str.Append(" , " + item);
  13. }
  14. this.lblList.Text = str.ToString();

复制代码

我们一起了解一下具体的类和接口之间的区别。

1. 具体类可继承其他类,并实现一个或多个接口。

2. 在内部类中可以定义变量并赋值,接口中不允许此操作。

3. 具体类可包含构造函数,而接口中不能定义构造函数

4. 抽象类中可包含访问修饰符如public,private等,接口中不能包含。

  1. //IEnumerable can not be instantiate from Enumerable , so it should be instantiate from List
  2. System.Collections.Generic.IEnumerable<Employee> empIEnumerable = new List<Employee>         {
  3. new Employee { ID = 1001, Name="Mahsa"},
  4. new Employee { ID = 1002, Name = "Hassankashi" },
  5. new Employee { ID = 1003, Name = "CosmicVerse" },
  6. new Employee { ID = 1004, Name = "Technical" }
  7. };
  8. this.GridViewIEnumerable.DataSource = empIEnumerable;
  9. this.GridViewIEnumerable.DataBind();
  10. System.Text.StringBuilder str = new System.Text.StringBuilder();
  11. foreach (Employee item in empIEnumerable)
  12. {
  13. str.Append(" , " + item.ID +"-"+item.Name);
  14. }
  15. this.lblIEnumerable.Text = str.ToString();

复制代码

九、IEnumerable

IEnumerable常用于遍历集合元素,但是无法修改(删除或添加)数据,使用IEnumberable 会从服务器端将所有数据拷贝到客户端,并进行一定的过滤,如果服务器端有大量数据会造成内存负载超重。

  1. //IEnumerable can not be instantiate from Enumerable , so it should be instantiate from List
  2. System.Collections.Generic.IEnumerable<Employee> empIEnumerable = new List<Employee>         {
  3. new Employee { ID = 1001, Name="Mahsa"},
  4. new Employee { ID = 1002, Name = "Hassankashi" },
  5. new Employee { ID = 1003, Name = "CosmicVerse" },
  6. new Employee { ID = 1004, Name = "Technical" }
  7. };
  8. this.GridViewIEnumerable.DataSource = empIEnumerable;
  9. this.GridViewIEnumerable.DataBind();
  10. System.Text.StringBuilder str = new System.Text.StringBuilder();
  11. foreach (Employee item in empIEnumerable)
  12. {
  13. str.Append(" , " + item.ID +"-"+item.Name);
  14. }
  15. this.lblIEnumerable.Text = str.ToString();

复制代码

十、IQueryable
IQueryable与IEnumberable不同的是,当从服务器端加载过量的数据,IQueryable会自动减少应用负载。IQueryable可保证大数据量时应用程序的高性能。IQueryable会先过滤数据,然后发送给客户端。

  1. DataAccessEntities ctx = new DataAccessEntities();
  2. var ctx = new DataAccessEntities();

复制代码

十一、SQL Profiler:

如何追踪查询语句生成TSQL,生成需要的数据结构体:

Step 1:

  1. Start -> MS SQL Server 2008 -> Performance Tools -> SQL Server Profiler

复制代码


Step 2:

  1. SQL Server Profiler -> File -> New Trace

复制代码

Step 3:

输入连接数据库的用户名和密码

Step 4:

  1. General (Tab) -> Use the Template: Standard

复制代码

Step 5:

  1. Event Selection (Tab) -> Event : TSQL -> Select : SQL-BatchCompleted | Select Show all Columns
  2. Press Column Filter -> Database Name: Like: "DataAccess"

复制代码

运行

Step 6:

查看结果

Step 7:
生成 IEnumerable数据 :

  1. SELECT
  2. [Extent1].[ID] AS [ID],
  3. [Extent1].[Name] AS [Name],
  4. [Extent1].[Age] AS [Age]
  5. FROM [dbo].[Employee] AS [Extent1]

复制代码

生成 IQueryable :

  1. SELECT
  2. [Extent1].[ID] AS [ID],
  3. [Extent1].[Name] AS [Name],
  4. [Extent1].[Age] AS [Age]
  5. FROM [dbo].[Employee] AS [Extent1]
  6. WHERE 1 = [Extent1].[ID]

复制代码

ICollection 继承了IEnumberable,但是IEnumberable是基于索引的,ICollection不基于索引。

十二、Stack Generic

入栈:

  1. //Stack is LIFO: Last in First Out
  2. //Here is for Push Stack in Generic
  3. //System.Collections.Stack objStackPush = new System.Collections.Stack();
  4. //Stack<T> can be instantiated from Stack<T>
  5. System.Collections.Generic.Stack<int> objStackPush = new System.Collections.Generic.Stack<int>();
  6. objStackPush.Push(1);
  7. objStackPush.Push(2);
  8. this.lblPopGeneric.Text = "";
  9. this.ListBoxStackGeneric.DataSource = objStackPush.ToArray();
  10. this.ListBoxStackGeneric.DataBind();

复制代码

Queue Generic

入队:

  1. //Stack is LIFO: Last in First Out
  2. //Here is for Pop Stack in Generic
  3. //System.Collections.Stack objStackPop = new System.Collections.Stack();
  4. //Stack<T> can be instantiated from Stack<T>
  5. System.Collections.Generic.Stack<int> objStackPop = new System.Collections.Generic.Stack<int>();
  6. objStackPop.Push(1);
  7. objStackPop.Push(2);
  8. this.lblPop.Text = objStackPop.Pop().ToString();
  9. this.ListBoxStack.DataSource = objStackPop.ToArray();
  10. this.ListBoxStack.DataBind();

复制代码

出队:

  1. //Queue is FIFO: First in First Out
  2. //Here is for Enqueue Queue in Generic
  3. //System.Collections.Queue objQueue = new System.Collections.Queue();
  4. //Queue<T> can be instantiated from Queue<T>
  5. System.Collections.Generic.Queue<int> objQueue = new System.Collections.Generic.Queue<int>();
  6. objQueue.Enqueue(1);
  7. objQueue.Enqueue(2);
  8. this.lblQueue.Text = "";
  9. this.ListBoxQueue.DataSource = objQueue.ToArray();
  10. this.ListBoxQueue.DataBind();

复制代码

十三、Dictionary 及 IDictionary:
Dictionary 可通用,而哈希表不是通用的。Dictionary定义 <TKey,Tvalue>。IDictionary是Dictionary的接口,如果在后期开发中需要大量修改,建议使用IDictionary。

  1. System.Collections.Generic.Dictionary<int, string=""> objDictionary = new Dictionary<int, string="">();
  2. objDictionary.Add(1001, "Mahsa");
  3. objDictionary.Add(1002, "Hassankashi");
  4. objDictionary.Add(1003, "Cosmicverse");
  5. string str = objDictionary[1002];
  6. this.ListBoxDictionary.DataSource = objDictionary;
  7. this.ListBoxDictionary.DataBind();</int,>

复制代码
来源:http://blog.csdn.net/long316/article/details/52595248

最全的数据结构解析与归纳相关推荐

  1. 【从蛋壳到满天飞】JS 数据结构解析和算法实现-AVL树(一)

    前言 [从蛋壳到满天飞]JS 数据结构解析和算法实现,全部文章大概的内容如下: Arrays(数组).Stacks(栈).Queues(队列).LinkedList(链表).Recursion(递归思 ...

  2. FreeMarker对应各种数据结构解析

    FreeMarker对应各种数据结构解析 FreeMarker 是一个采用 Java 开发的模版引擎,是一个基于模版生成文本的通用工具. FreeMarker 被设计用来生成 HTML Web 页面, ...

  3. 【运筹学】线性规划 单纯形法 阶段总结 ( 初始基可行解 | 判定最优解 | 迭代 | 得到最优解 | 全流程详细解析 ) ★

    文章目录 一.线性规划示例 二.转化标准形式 三.查找初始基可行解 四.初始基可行解的最优解判定 五.第一次迭代 : 入基与出基变量选择 六.第一次迭代 : 方程组同解变换 七.第一次迭代 : 生成新 ...

  4. 【从蛋壳到满天飞】JS 数据结构解析和算法实现-哈希表

    前言 [从蛋壳到满天飞]JS 数据结构解析和算法实现,全部文章大概的内容如下: Arrays(数组).Stacks(栈).Queues(队列).LinkedList(链表).Recursion(递归思 ...

  5. 什么叫返回路径平面上的间隙_差分信号回流路径的全波电磁场解析

    差分信号回流路径的全波电磁场解析 差分信号回流路径的全波电磁场解析 xxx究所EDA设计部  xxx 摘要: 本文以高速系统的差分信号回流路径为基本出发点, 着重介绍了差分信号的参考平面的开 槽间隙对 ...

  6. 【从蛋壳到满天飞】JS 数据结构解析和算法实现-堆和优先队列(一)

    前言 [从蛋壳到满天飞]JS 数据结构解析和算法实现,全部文章大概的内容如下: Arrays(数组).Stacks(栈).Queues(队列).LinkedList(链表).Recursion(递归思 ...

  7. Live800:企业必修课|新时代的全渠道营销解析(上)

    众所周知,随机用户在成为忠实客户之前,通常需要多次接触才能孵化出销售机会.在很多年以前,电视广告.电子邮件.线下商店等渠道可以轻松触达大多数用户,这个时代被称为大众营销传播时代. 而如今小程序.直播. ...

  8. Live800:企业必修课|新时代的全渠道营销解析(下)

    接上文Live800:企业必修课|新时代的全渠道营销解析(上) 三.全渠道营销的应用 理念变化:消费者和顾客转变为用户 在完整的全渠道体系中,企业需要重点建设数字化平台,将消费者.顾客转变为用户.哪怕 ...

  9. AVI音视频封装格式学习(三)——AVI 数据结构解析

    这里介绍AVI会使用到的数据结构,为了避免翻译引入歧义,决定该部分还是使用英文原文,如后续有时间再进行翻译. AVIMAINHEADER structure The AVIMAINHEADER str ...

最新文章

  1. 空间点像素索引(三)
  2. linux桌面版排行2019_2019 年适合新手的 Linux 发行版 top 5
  3. Struts2中的Action
  4. 有哪些网站是django开发的_网站模板建设和定制开发哪个好,有哪些区别?
  5. k8s核心技术-持久化存储(nfs网络存储)---K8S_Google工作笔记0050
  6. 谈一下ACM的入门书籍及方法
  7. 你确认退出吗 html,按退出会 执行2次弹出确认窗口,为何?
  8. 运用正则表达式在Asp中过滤Html标签代码的四种不同方法
  9. java从入门到进阶
  10. gensler逻辑学导论_学逻辑学,哪本书入门合适?
  11. linux创建2g文件,创建一个2G的文件(Linux命令dd)
  12. 深度学习入门:基于Python的理论与实现——第一章Python入门
  13. autojs 串口通信 替代无障碍 串口
  14. 低速常温离心机S400
  15. Win11的几个实用技巧系列之不能玩植物大战僵尸、如何彻底删除360所有文件
  16. 微信小程序父子页面间得数据传递(对象或者数组)
  17. 实战 Java 第10天:商品分页查询
  18. phpspreadsheet 读取 Excel 表格问题
  19. 自动判卷 、答题卡识别、六级答题卡客观题自动判卷系统1.0
  20. windows system32和sysWOW64文件下面的dll丢失-解决方案

热门文章

  1. android自助终端界面_ZTHP500 | 桌面式人脸消费终端
  2. html fmt转换日期格式,js通用时间格式转换函数
  3. 雷讯和pix_青海叶陇沟金矿地质地球化学特征及找矿方向
  4. 关于Maven中的常见命令,通过命令的方式快速创建一个空的maven工程,将jar包打到maven仓库中
  5. 关于Eclipse创建Android项目时,会多出一个appcompat_v7的问题
  6. ContextLoaderListener的用途以及配置方式
  7. Java Swing编程:JTable表格
  8. 深入浅出Yolov5之自有数据集训练超详细教程
  9. Faster-RCNN训练时遇到的问题
  10. SD初始化过程以及Cmd解析