C#中数组Array,ArrayList,泛型List详细对比
作者:hebedich 发布时间:2023-02-22 05:44:33
在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析。
一、数组Array
数组是一个存储相同类型元素的固定大小的顺序集合。数组是用来存储数据的集合,通常认为数组是一个同一类型变量的集合。
Array 类是 C# 中所有数组的基类,它是在 System 命名空间中定义。
数组在内存中是连续存储的,所以它的索引速度非常快,而且赋值与修改元素也非常简单。
Array数组具体用法:
using System;
namespace WebApp
{
class Program
{
static void Main(string[] args)
{
//System.Array
//1、数组[] 特定类型、固定长度
string[] str1 = new string[3];
str1[0] = "a";
str1[1] = "b";
str1[2] = "c";
Console.WriteLine(str1[2]);
string[] str2 = new string[] { "a", "b", "c" };
Console.WriteLine(str2[0]);
string[] str3 = { "a", "b", "c" };
Console.WriteLine(str3[0]);
//2、二维数组
//int[,] intArray=new int[2,3]{{1,11,111},{2,22,222}};
int[,] intArray = new int[2, 3];
intArray[0, 0] = 1;
intArray[0, 1] = 11;
intArray[0, 2] = 111;
intArray[1, 0] = 2;
intArray[1, 1] = 22;
intArray[1, 2] = 222;
Console.WriteLine("{0},{1},{2}", intArray[0, 0], intArray[0, 1], intArray[0, 2]);
Console.WriteLine("{0},{1},{2}", intArray[1, 0], intArray[1, 1], intArray[1, 2]);
//3、多维数组
int[, ,] intArray1 = new int[,,]
{
{{1, 1}, {11, 11}, {111, 111}},
{{2, 2}, {22, 22}, {222, 222}},
{{3, 3}, {33, 33}, {333, 333}}
};
Console.WriteLine("{0},{1},{2},{3},{4},{5}", intArray1[0, 0, 0], intArray1[0, 0, 1], intArray1[0, 1, 0], intArray1[0, 1, 1],
intArray1[0, 2, 0], intArray1[0, 2, 1]);
Console.WriteLine("{0},{1},{2},{3},{4},{5}", intArray1[1, 0, 0], intArray1[1, 0, 1], intArray1[1, 1, 0], intArray1[1, 1, 1],
intArray1[1, 2, 0], intArray1[1, 2, 1]);
Console.WriteLine("{0},{1},{2},{3},{4},{5}", intArray1[2, 0, 0], intArray1[2, 0, 1], intArray1[2, 1, 0], intArray1[2, 1, 1],
intArray1[2, 2, 0], intArray1[2, 2, 1]);
//4、交错数组即数组的数组
int[][] intArray2 = new int[4][];
intArray2[0] = new int[] { 1 };
intArray2[1] = new int[] { 2, 22 };
intArray2[2] = new int[] { 3, 33, 333 };
intArray2[3] = new int[] { 4, 44, 444,4444 };
for (int i = 0; i < intArray2.Length; i++)
{
for (int j = 0; j < intArray2[i].Length; j++)
{
Console.WriteLine("{0}", intArray2[i][j]);
}
}
Console.ReadKey();
}
}
}
数组虽然存储检索数据很快,但是也有一些缺点:
1、在声明数组的时候必须指定数组的长度,如果不清楚数组的长度,就会变得很麻烦。
2、数组的长度太长,会造成内存浪费;太短会造成数据溢出的错误。
3、在数组的两个数据间插入数据是很麻烦的
更多参考微软官方文档:Array 类 (System)
二、ArrayList
既然数组有很多缺点,C#就提供了ArrayList对象来克服这些缺点。
ArrayList是在命名空间System.Collections下,在使用该类时必须进行引用,同时继承了IList接口,提供了数据存储和检索。
ArrayList对象的大小是按照其中存储的数据来动态扩充与收缩的。因此在声明ArrayList对象时并不需要指定它的长度。
ArrayList 的默认初始容量为 0。随着元素添加到 ArrayList 中,容量会根据需要通过重新分配自动增加。可通过调用 TrimToSize 或通过显式设置 Capacity 属性减少容量。
using System;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
ArrayList myAL = new ArrayList();
myAL.Add("Hello");
myAL.Add("World");
myAL.Add("!");
Console.WriteLine( "myAL" );
Console.WriteLine( " Count: {0}", myAL.Count );
Console.WriteLine( " Capacity: {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );
}
public static void PrintValues( IEnumerable myList ) {
foreach ( Object obj in myList )
Console.Write( " {0}", obj );
Console.WriteLine();
Console.ReadKey();
}
}
运行结果:
ArrayList解决了数组中所有的缺点,但是在存储或检索值类型时通常发生装箱和取消装箱操作,带来很大的性能耗损。尤其是装箱操作,例如:
ArrayList list = new ArrayList();
//add
list.Add("joye.net");
list.Add(27);
//update
list[2] = 28;
//delete
list.RemoveAt(0);
//Insert
list.Insert(0, "joye.net1");
在List中,先插入了字符串joye.net,而且插入了int类型27。这样在ArrayList中插入不同类型的数据是允许的。因为ArrayList会把所有插入其中的数据当作为object类型来处理,在使用ArrayList处理数据时,很可能会报类型不匹配的错误,也就是ArrayList不是类型安全的。
更多参考微软官方ArrayList文档:ArrayList 类 (System.Collections)
三、泛型List<T>
由于ArrayList存在不安全类型与装箱拆箱的缺点,所以出现了泛型的概念。
List 类是 ArrayList 类的泛型等效类。该类使用大小可按需动态增加的数组实现 IList 泛型接口,大部分用法都与ArrayList相似。
List<T> 是类型安全的,在声明List集合时,必须为其声明List集合内数据的对象类型。
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<string> dinosaurs = new List<string>();
Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);
dinosaurs.Add("Tyrannosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Compsognathus");
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);
Console.WriteLine("Count: {0}", dinosaurs.Count);
Console.WriteLine("\nContains(\"Deinonychus\"): {0}",
dinosaurs.Contains("Deinonychus"));
Console.WriteLine("\nInsert(2, \"Compsognathus\")");
dinosaurs.Insert(2, "Compsognathus");
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\ndinosaurs[3]: {0}", dinosaurs[3]);
Console.WriteLine("\nRemove(\"Compsognathus\")");
dinosaurs.Remove("Compsognathus");
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
dinosaurs.TrimExcess();
Console.WriteLine("\nTrimExcess()");
Console.WriteLine("Capacity: {0}", dinosaurs.Capacity);
Console.WriteLine("Count: {0}", dinosaurs.Count);
dinosaurs.Clear();
Console.WriteLine("\nClear()");
Console.WriteLine("Capacity: {0}", dinosaurs.Capacity);
Console.WriteLine("Count: {0}", dinosaurs.Count);
}
}
如果声明List集合内数据的对象类型是string,然后往List集合中插入int类型的111,IDE就会报错,且不能通过编译。显然这样List<T>是类型安全的。
对返回结果集再封装:
public class ResultDTO<T>
{
public T Data { get; set; }
public string Code { get; set; }
public string Message { get; set; }
}
var data = new CityEntity();
return new ResultDTO<CityEntity> { Data = data, Code = "1", Message = "sucess"};
var data2 = new List<CityEntity>();
return new ResultDTO<List<CityEntity>> { Data = data2, Code = "1", Message = "sucess" };
var data1 = 1;
return new ResultDTO<int> { Data = data1, Code = "1", Message = "sucess" };
更多参考微软官方文档:List泛型类
四、总结
1、数组的容量固定,而ArrayList或List<T>的容量可根据需要自动扩充。
2、数组可有多个维度,而 ArrayList或 List< T> 始终只有一个维度。(可以创建数组列表或列表的列表)
3、特定类型的数组性能优于 ArrayList的性能(不包括Object,因为 ArrayList的元素是 Object ,在存储或检索值类型时通常发生装箱和取消装箱操作)。
4、 ArrayList 和 List<T>基本等效,如果List< T> 类的类型T是引用类型,则两个类的行为是完全相同的。如果T是值类型,需要考虑装箱和拆箱造成的性能损耗。List<T> 是类型安全。


猜你喜欢
- 目录1 简介2 Condition的实现分析等待队列等待(await):AbstractQueuedLongSynchronizer中实现C
- 本文实例为大家分享了Unity UI实现拖拽旋转的具体代码,供大家参考,具体内容如下跟随鼠标旋转第一种效果是跟随鼠标旋转,原理是计算下鼠标位
- 本文实例为大家分享了Java操作qq邮箱发送邮件的具体代码,供大家参考,具体内容如下今天尝试了使用QQ邮箱的POP3/IMAP/SMTP/E
- 最近在做一个需求:从其他系统的ftp目录下载存储图片url的文件,然后读取文件中的url地址,根据地址下载图片后按天压缩成一个包,平均一个地
- .net内存回收与Dispose﹐Close﹐Finalize方法一. net的对象使用一般分为三种情况﹕1.创建对象2.使用对象3.释放对
- springcloud微服务包含的技术种类众多,eureka作为其注册中心,一直处于主流,但在今年已经处于永久停更状态,但其优秀的能力还是值
- 字符串广泛应用 在 Java 编程中,在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串。深刻认识Stri
- Java与C++实现相同的MD5加密算法1、Java版package com.lyz.utils.common;import java.io
- 1.使用matlab作闭合多边形图没有找到直接画多边形的函数,只能是将各个点的坐标保存在数组中,将一个点与其相邻的点相连,并将最后一个点与第
- SpringBoot配置https(SSL证书)最近在做微信小程序,https是必须条件仅需三步SpringBoot2.x版本对比一下这个小
- 本文实例介绍了sdcard存储图片下载简单操作,分享给大家供大家参考,具体内容如下步骤 -- 在配置清单添加完联网权限后1、res/layo
- 上期回顾上期我们主要介绍了排序的基本认识,以及四个排序,分别是直接插入排序,希尔排序,选择排序,堆排序,从这些排序中,了解了算法的实现,以及
- 字符串的操作是C#程序设计中十分重要的一个组成部分,本文就以实例形式展现了C#实现移除字符串末尾指定字符的方法。相信对大家学习C#程序设计有
- 解决@NotBlank不生效在项目开发中,发现一个类中包含有另外一个类,这种包含关系的类上的@NotBlank校验不生效,后来发现需要在内部
- 我们都知道Android应用软件基本上都会用到登录注册功能,那么对一个一个好的登录注册模块进行封装就势在必行了。这里给大家介绍一下我的第一个
- 前言先说结论,tauri是一个非常优秀的前端桌面开发框架,但是,rust门槛太高了。一开始我是用electron来开发的,但是打包后发现软件
- 本文实例为大家分享了shiro整合springboot前后端分离的具体代码,供大家参考,具体内容如下1、shiro整合springboot的
- 环境:SpringFramework:4.3.5.RELEASEapollo-client:1.5.11.在项目的 resources/ME
- 本文实例讲述了Android显示网络图片的方法,分享给大家供大家参考。具体方法如下:一般来说,在Android中显示一张网络图片其实是非常简
- 实现效果为一个小球接触左右侧时,会反向的运动。import javafx.application.Application;import ja