国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#中數組Array,ArrayList,泛型List詳細對比

2019-10-29 21:26:00
字體:
來源:轉載
供稿:網友

在C#中數組Array,ArrayList,泛型List都能夠存儲一組對象,但是在開發中根本不知道用哪個性能最高,下面我們慢慢分析分析。

一、數組Array

數組是一個存儲相同類型元素的固定大小的順序集合。數組是用來存儲數據的集合,通常認為數組是一個同一類型變量的集合。

Array 類是 C# 中所有數組的基類,它是在 System 命名空間中定義。

數組在內存中是連續存儲的,所以它的索引速度非??欤屹x值與修改元素也非常簡單。

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> 是類型安全。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南宫市| 莱西市| 富平县| 且末县| 扶风县| 平舆县| 铜川市| 日照市| 翁源县| 方正县| 尖扎县| 天长市| 雅安市| 陕西省| 怀仁县| 缙云县| 衡阳县| 光山县| 金门县| 佛学| 都江堰市| 柞水县| 潮州市| 沙湾县| 常宁市| 濮阳县| 高清| 临清市| 奎屯市| 子长县| 镇巴县| 南华县| 汽车| 南宫市| 河曲县| 区。| 莱州市| 洛川县| 白朗县| 深泽县| 广宁县|