在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[];
str1[] = "a";
str1[] = "b";
str1[] = "c";
Console.WriteLine(str1[]); string[] str2 = new string[] { "a", "b", "c" };
Console.WriteLine(str2[]); string[] str3 = { "a", "b", "c" };
Console.WriteLine(str3[]);
//2、二维数组
//int[,] intArray=new int[2,3]{{1,11,111},{2,22,222}};
int[,] intArray = new int[, ];
intArray[, ] = ;
intArray[, ] = ;
intArray[, ] = ; intArray[, ] = ;
intArray[, ] = ;
intArray[, ] = ;
Console.WriteLine("{0},{1},{2}", intArray[, ], intArray[, ], intArray[, ]);
Console.WriteLine("{0},{1},{2}", intArray[, ], intArray[, ], intArray[, ]); //3、多维数组
int[, ,] intArray1 = new int[,,]
{
{{, }, {, }, {, }},
{{, }, {, }, {, }},
{{, }, {, }, {, }}
};
Console.WriteLine("{0},{1},{2},{3},{4},{5}", intArray1[, , ], intArray1[, , ], intArray1[, , ], intArray1[, , ],
intArray1[, , ], intArray1[, , ]);
Console.WriteLine("{0},{1},{2},{3},{4},{5}", intArray1[, , ], intArray1[, , ], intArray1[, , ], intArray1[, , ],
intArray1[, , ], intArray1[, , ]);
Console.WriteLine("{0},{1},{2},{3},{4},{5}", intArray1[, , ], intArray1[, , ], intArray1[, , ], intArray1[, , ],
intArray1[, , ], intArray1[, , ]); //4、交错数组即数组的数组
int[][] intArray2 = new int[][];
intArray2[] = new int[] { };
intArray2[] = new int[] { , };
intArray2[] = new int[] { , , };
intArray2[] = new int[] { , , , };
for (int i = ; i < intArray2.Length; i++)
{
for (int j = ; 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(); //update
list[] = ; //delete
list.RemoveAt(); //Insert
list.Insert(, "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(, "Compsognathus"); Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
} Console.WriteLine("\ndinosaurs[3]: {0}", dinosaurs[]); 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 = "", Message = "sucess"}; var data2 = new List<CityEntity>();
return new ResultDTO<List<CityEntity>> { Data = data2, Code = "", Message = "sucess" }; var data1 = ;
return new ResultDTO<int> { Data = data1, Code = "", 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> 是类型安全。

C#中数组Array、ArrayList、泛型List<T>的比较的更多相关文章

  1. 将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)

    方法一:使用Arrays.asList()方法   1 2 String[] asset = {"equity", "stocks", "gold&q ...

  2. C#中数组、ArrayList和List三者的区别

    在C#中数组,ArrayList,List都能够存储一组对象,那么这三者到底有什么样的区别呢. 数组 数组在C#中最早出现的.在内存中是连续存储的,所以它的索引速度非常快,而且赋值与修改元素也很简单. ...

  3. C#中数组、ArrayList和List<T>三者的发展历程

    在C#中数组,ArrayList,List使我们用的最多的类型之一.他们共同的作用都是能够存储一组对象. 那么问题来了: (1)为什么要有三个一样作用的东西呢?他们都很完美吗? (2)谁先出生,又是因 ...

  4. C#中数组、ArrayList和List三者的区别 转

    在C#中数组,ArrayList,List都能够存储一组对象,那么这三者到底有什么样的区别呢. 数组 数组在C#中最早出现的.在内存中是连续存储的,所以它的索引速度非常快,而且赋值与修改元素也很简单. ...

  5. 【转载】 C#中数组、ArrayList和List三者的区别

    原文地址:http://blog.csdn.net/zhang_xinxiu/article/details/8657431 在C#中数组,ArrayList,List都能够存储一组对象,那么这三者到 ...

  6. 问题:C# List;结果:C#中数组、ArrayList和List三者的区别

    C#中数组.ArrayList和List三者的区别 分类: [C#那些事] 2013-03-11 00:03 36533人阅读 评论(23) 收藏 举报 目录(?)[+] 在C#中数组,ArrayLi ...

  7. (转)C#中数组、ArrayList和List三者的区别

    原文地址:http://blog.csdn.net/zhang_xinxiu/article/details/8657431 在C#中数组,ArrayList,List都能够存储一组对象,那么这三者到 ...

  8. JavaScript中数组Array方法详解

    ECMAScript 3在Array.prototype中定义了一些很有用的操作数组的函数,这意味着这些函数作为任何数组的方法都是可用的. 1.Array.join()方法 Array.join()方 ...

  9. C#中数组,ArrayList与List对象的区别

    在C#中,当我们想要存储一组对象的时候,就会想到用数组,ArrayList,List这三个对象了.那么这三者到底有什么样的区别呢? 我们先来了解一下数组,因为数组在C#中是最早出现的. 数组 数组有很 ...

随机推荐

  1. SQL Server(七)——存储过程

    一.概述 存储过程是一组编译在单个执行计划中的T-SQL语句 存储过程:就像函数一样的会保存在数据库中(可编程性) 存储过程的优点: 1.允许模块化程序设计 2.允许更快执行如果某操作需要大量T-SQ ...

  2. PHP5中的stdClass

    PHP5中新增stdClass 官方手册参考:http://www.php.net/manual/en/language.oop5.basic.php#92123 stdClass类是PHP的一个内部 ...

  3. 按要求编写Java应用程序。 (1)创建一个叫做机动车的类: 属性:车牌号(String),车速(int),载重量(double) 功能:加速(车速自增)、减速(车速自减)、修改车牌号,查询车的载重量。 编写两个构造方法:一个没有形参,在方法中将车牌号设置“XX1234”,速 度设置为100,载重量设置为100;另一个能为对象的所有属性赋值; (2)创建主类: 在主类中创建两个机动车对象。 创建第

    package com.hanqi.test; public class jidongche { private String chepaihao;//车牌号 private int speed;// ...

  4. 0008《SQL必知必会》笔记04-子查询、联接与组合查询

    1.子查询:就是嵌套的查询,用一个查询的结果作为另一个查询的条件. 比如要列出订购了物品“RGAN01”的所有顾客的ID.姓名和联系人,需要经历以下几步 (1)从orderitems中找出订购了“RG ...

  5. SqlServer链接MySql操作步骤

    Sql Server版本 2008R2 1.从MySQL网站下载最新的MySQL ODBC驱动:http://www.mysql.com/downloads/connector/odbc/,我下载的版 ...

  6. Java 使用 JRegistry-1.8.1 读取和设置 windows 注册表

    在一个监控相关的Java项目中,需要读取windows系统的注册表,搜索到使用 JRegistery 可以解决.代码如下: /** * @author digdeep@126.com */ publi ...

  7. android中ADT和SDK的关系

    ADT(Android Development Tools): 目前Android开发所用的开发工具是Eclipse,在Eclipse编译IDE环境中,安装ADT,为Android开发提供开发工具的升 ...

  8. linux开机自动连接无线网络

           1.右击无线网络图标的“编辑连接”. 2.在“无线”选项卡里,选择“编辑”. 3.在“无线安全性”选项卡里,输入无线密匙,并选中左下角的“对所有用户可      用”的选项点击应用,会提 ...

  9. shell脚本学习指南

    一.UNIX各个工具所支持的正则表达式: 二.使用sed在某一目录下建立一份目录备份: find /home/foo/ -type d -print | #找出/home/foo/目录下所有目录文件 ...

  10. Linux vim(4)

    vim相对于vi做了很多的提升: -VIM支持多集撤销 -VIM可以跨平台运行 —VIM支持语法高亮 -VIM支持图形界面 vim三大模式: command mode vi进入时的默认模式 esc进入 ...