当前位置:网站首页>C#中,数组、List、ArrayList、Directory、LinkedList的区别与性能
C#中,数组、List、ArrayList、Directory、LinkedList的区别与性能
2022-06-22 11:30:00 【草原上唱山歌】
数组:int[] count = new int[10];
List:List<int> list = new List<int>;
ArrayList:ArrayList arrayList = new ArrayList();
Dictionary:Dictionary<string, int> dire = new Dictionary<string, int>();
LinkedList:LinkedlList<string> linkedList = new LinkedList<string>();
分析:
从上面的初始化可以看出,这几种类型都是属于引用类型。其中数组、List、Dictionary和LinkedList在初始化时需要指定其元素类型(数组还需要指定其大小),而ArrayList在初始化时不需要指定其类型。
数组:在初始化时必须指定其大小和类型,它在内存中是连续存储的,所以可以看出数组的索引速度是非常快的。在确定了数组的长度和类型后,选择数组存储数据是比较好的选择。不适合插入操作。
List:在初始化的时候必须指定集合内对象的数据类型,但是不需要指定大小,它不会像ArraryList那样在存取过程中引起装箱和拆箱操作,避免了类型不安全性。在类型相同的情况下,List和数组的性能相当。插入操作方便。
ArrayList:ArrayList在System.Collections命名空间下,同时继承了IList接口。ArrayList对象的大小可按照存储的数据进行动态增减,在初始化的时候不需要指定其大小和类型。针对数组在两个数据之间插入数据较麻烦,而且初始化时必须确定其大小,因此C#提供ArrayList克服这些缺陷,它可以存储不同的数据类型,存取的所有数据会转化为object类型,插入操作方便,但是在对值类型进行存取和检索时,会引起装箱和拆箱,降低了性能,同时ArrayList没有泛型的实现,它不是类型安全的,在处理数据时很有可能产生数据不匹配的错误。
Dictionary:在初始化的时候也必须指定其类型,而且它还需要指定一个Key,并且这个Key是唯一的。正因为这样,Dictionary的索引速度非常快。但是也因为它增加了一个Key,Dictionary占用的内存空间比其它类型要大。它是通过Key来查找元素的,元素的顺序是不定的。
LinkedList:数组和数组列表都有一个重大的缺陷,这就是从数组的中间位置删除一个元素需要付出很大的代价,其原因是数组中处于被删除元素之后的所有元素都要向数组的前端移动。在数组的中间的位置插入一个元素也是如此。
边栏推荐
- 配置GPU版本的pytorch和torchvision,初学GPU版本torch踩坑
- Three transition schemes from IPv4 to IPv6
- Bytearraystream case of IO
- Call center terminology
- Collection of IO operation cases
- Fight, programmer - Chapter 37 The Xiongguan pass is like an iron road. Now, I'm going to cross it from the beginning
- How to improve customer conversion rate on the official website
- 奋斗吧,程序员——第三十八章 旧时茅店社林边,路转溪头忽见
- 奋斗吧,程序员——第三十六章 落花人独立,微雨燕双飞
- 克鲁斯卡尔重构树
猜你喜欢
随机推荐
lyndon分解学习笔记
奋斗吧,程序员——第四十七章 所谓伊人,在水一方
[安洵杯 2019]iamthinking
TiFlash 函数下推必知必会丨十分钟成为 TiFlash Contributor
Development technology of NFT trading platform digital collection system
2022过半,没有新风口
What is the image used to parse the Tso of the DN binlog? It seems that there is no direct use of mysqlbinlog?
Typical life cycle model of information system project
成功案例 | 安超云助力兰州大学第二医院搭建新型IT基础设施平台 提升医疗信息资源利用率
宏定义使用以及typedef和const
牛客挑战赛53C
【软工】 设计模块
Redis - 12、应用问题解决
HMS Core新闻行业解决方案:让技术加上人文的温度
Exchange the nodes in the linked list in pairs [the principle of one-way linked list without chain]
Struggle, programmer chapter 38 in the old days, when the road turned to the edge of the forest, I suddenly saw
企业官网如何提高客户转化率
奋斗吧,程序员——第四十八章 千金纵买相如赋,脉脉此情谁诉
鉴权之cookie、session、JWT
什么是同源???跨域错误???如何解决???


![两两交换链表中的节点[单向链表不断链原则]](/img/67/8e9f3c396a8f529a616964b69cc47f.png)





