最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#中數(shù)組Array,ArrayList,泛型List詳細(xì)對(duì)比

 更新時(shí)間:2016年06月17日 10:03:18   投稿:hebedich  
關(guān)于數(shù)組Array,ArrayList,泛型List,簡(jiǎn)單的說數(shù)組就是值對(duì)象,它存儲(chǔ)數(shù)據(jù)元素類型的值的一系列位置.Arraylist和list可以提供添加,刪除,等操作的數(shù)據(jù). 具體如何進(jìn)行選擇使用呢,我們來詳細(xì)探討下

在C#中數(shù)組Array,ArrayList,泛型List都能夠存儲(chǔ)一組對(duì)象,但是在開發(fā)中根本不知道用哪個(gè)性能最高,下面我們慢慢分析分析。

一、數(shù)組Array

數(shù)組是一個(gè)存儲(chǔ)相同類型元素的固定大小的順序集合。數(shù)組是用來存儲(chǔ)數(shù)據(jù)的集合,通常認(rèn)為數(shù)組是一個(gè)同一類型變量的集合。

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

數(shù)組在內(nèi)存中是連續(xù)存儲(chǔ)的,所以它的索引速度非常快,而且賦值與修改元素也非常簡(jiǎn)單。

Array數(shù)組具體用法:

using System;

namespace WebApp
{
  class Program
  {
    static void Main(string[] args)
    {
      //System.Array
      //1、數(shù)組[] 特定類型、固定長(zhǎng)度
      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、二維數(shù)組
      //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、多維數(shù)組
      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、交錯(cuò)數(shù)組即數(shù)組的數(shù)組
      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();
    }
  }
}

數(shù)組雖然存儲(chǔ)檢索數(shù)據(jù)很快,但是也有一些缺點(diǎn):

1、在聲明數(shù)組的時(shí)候必須指定數(shù)組的長(zhǎng)度,如果不清楚數(shù)組的長(zhǎng)度,就會(huì)變得很麻煩。

2、數(shù)組的長(zhǎng)度太長(zhǎng),會(huì)造成內(nèi)存浪費(fèi);太短會(huì)造成數(shù)據(jù)溢出的錯(cuò)誤。

3、在數(shù)組的兩個(gè)數(shù)據(jù)間插入數(shù)據(jù)是很麻煩的

更多參考微軟官方文檔:Array 類 (System)

二、ArrayList

既然數(shù)組有很多缺點(diǎn),C#就提供了ArrayList對(duì)象來克服這些缺點(diǎn)。

ArrayList是在命名空間System.Collections下,在使用該類時(shí)必須進(jìn)行引用,同時(shí)繼承了IList接口,提供了數(shù)據(jù)存儲(chǔ)和檢索。

ArrayList對(duì)象的大小是按照其中存儲(chǔ)的數(shù)據(jù)來動(dòng)態(tài)擴(kuò)充與收縮的。因此在聲明ArrayList對(duì)象時(shí)并不需要指定它的長(zhǎng)度。

ArrayList 的默認(rèn)初始容量為 0。隨著元素添加到 ArrayList 中,容量會(huì)根據(jù)需要通過重新分配自動(dòng)增加??赏ㄟ^調(diào)用 TrimToSize 或通過顯式設(shè)置 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();
  }

}

運(yùn)行結(jié)果:

ArrayList解決了數(shù)組中所有的缺點(diǎn),但是在存儲(chǔ)或檢索值類型時(shí)通常發(fā)生裝箱和取消裝箱操作,帶來很大的性能耗損。尤其是裝箱操作,例如:

      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中插入不同類型的數(shù)據(jù)是允許的。因?yàn)锳rrayList會(huì)把所有插入其中的數(shù)據(jù)當(dāng)作為object類型來處理,在使用ArrayList處理數(shù)據(jù)時(shí),很可能會(huì)報(bào)類型不匹配的錯(cuò)誤,也就是ArrayList不是類型安全的。

更多參考微軟官方ArrayList文檔:ArrayList 類 (System.Collections)

三、泛型List<T>

由于ArrayList存在不安全類型與裝箱拆箱的缺點(diǎn),所以出現(xiàn)了泛型的概念。

List 類是 ArrayList 類的泛型等效類。該類使用大小可按需動(dòng)態(tài)增加的數(shù)組實(shí)現(xiàn) IList 泛型接口,大部分用法都與ArrayList相似。

List<T> 是類型安全的,在聲明List集合時(shí),必須為其聲明List集合內(nèi)數(shù)據(jù)的對(duì)象類型。

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集合內(nèi)數(shù)據(jù)的對(duì)象類型是string,然后往List集合中插入int類型的111,IDE就會(huì)報(bào)錯(cuò),且不能通過編譯。顯然這樣List<T>是類型安全的。

對(duì)返回結(jié)果集再封裝:

  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泛型類

四、總結(jié)

1、數(shù)組的容量固定,而ArrayList或List<T>的容量可根據(jù)需要自動(dòng)擴(kuò)充。

2、數(shù)組可有多個(gè)維度,而 ArrayList或 List< T> 始終只有一個(gè)維度。(可以創(chuàng)建數(shù)組列表或列表的列表)

3、特定類型的數(shù)組性能優(yōu)于 ArrayList的性能(不包括Object,因?yàn)?ArrayList的元素是 Object ,在存儲(chǔ)或檢索值類型時(shí)通常發(fā)生裝箱和取消裝箱操作)。

4、 ArrayList 和 List<T>基本等效,如果List< T> 類的類型T是引用類型,則兩個(gè)類的行為是完全相同的。如果T是值類型,需要考慮裝箱和拆箱造成的性能損耗。List<T> 是類型安全。

相關(guān)文章

  • c# EnumHelper枚舉常用操作類

    c# EnumHelper枚舉常用操作類

    在項(xiàng)目中需要把枚舉填充到下拉框中,所以使用統(tǒng)一的方法實(shí)現(xiàn),測(cè)試代碼如下,需要的朋友可以參考下
    2016-11-11
  • 基于C#方法重載的總結(jié)詳解

    基于C#方法重載的總結(jié)詳解

    本篇文章是對(duì)C#中方法重載進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • visio二次開發(fā)--判斷文檔是否已發(fā)生變化(變化就加星號(hào)*)

    visio二次開發(fā)--判斷文檔是否已發(fā)生變化(變化就加星號(hào)*)

    最近做一個(gè)故障樹診斷的項(xiàng)目,用visio二次開發(fā),可以同時(shí)打開多個(gè)繪制的故障樹圖形文檔。項(xiàng)目中需要實(shí)現(xiàn)判斷文檔是否發(fā)生變化,這是很多編輯軟件的基本功能,變化了就加個(gè)星號(hào)*
    2013-04-04
  • C#中實(shí)現(xiàn)屏蔽Ctrl+C的方法

    C#中實(shí)現(xiàn)屏蔽Ctrl+C的方法

    這篇文章主要介紹了C#中實(shí)現(xiàn)屏蔽Ctrl+C的方法,在C#應(yīng)用程序開發(fā)中有一定的實(shí)用價(jià)值,需要的朋友可以參考下
    2014-08-08
  • C#自定義轉(zhuǎn)換器的實(shí)現(xiàn)

    C#自定義轉(zhuǎn)換器的實(shí)現(xiàn)

    本文主要介紹了C#自定義轉(zhuǎn)換器的實(shí)現(xiàn),包括隱式轉(zhuǎn)換和顯式轉(zhuǎn)換的語法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • WPF實(shí)現(xiàn)播放RTSP視頻流

    WPF實(shí)現(xiàn)播放RTSP視頻流

    這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)播放RTSP視頻流的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-01-01
  • 基于C#編寫經(jīng)理評(píng)分系統(tǒng)

    基于C#編寫經(jīng)理評(píng)分系統(tǒng)

    最近接了這樣一個(gè)項(xiàng)目,要求使用c#編寫經(jīng)理評(píng)分系統(tǒng),需求,要顯示員工信息,實(shí)現(xiàn)項(xiàng)目經(jīng)理給員工評(píng)分功能,今天小編分步驟給大家介紹,需要的的朋友參考下
    2017-03-03
  • c#獲取季度時(shí)間實(shí)例代碼(季度的第一天)

    c#獲取季度時(shí)間實(shí)例代碼(季度的第一天)

    這篇文章主要介紹了c#獲取季度時(shí)間:季度的第一天、季度的最后一天等功能,大家參考使用吧
    2013-12-12
  • 深入了解C#設(shè)計(jì)模式之訂閱發(fā)布模式

    深入了解C#設(shè)計(jì)模式之訂閱發(fā)布模式

    這篇文章主要介紹了C#設(shè)計(jì)模式之訂閱發(fā)布模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • C# 腳本引擎CS-Script的使用

    C# 腳本引擎CS-Script的使用

    這篇文章主要介紹了C#腳本引擎CS-Script的使用,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-12-12

最新評(píng)論

建瓯市| 浏阳市| 铁岭县| 左贡县| 日喀则市| 温宿县| 开阳县| 邳州市| 丰镇市| 郸城县| 商洛市| 合肥市| 合作市| 凌源市| 海盐县| 蕉岭县| 英德市| 梁河县| 萨嘎县| 通江县| 云南省| 格尔木市| 白城市| 天峻县| 湟中县| 青海省| 尉氏县| 抚宁县| 阳山县| 新竹县| 汶川县| 文化| 屏山县| 东乌珠穆沁旗| 扬中市| 峨边| 哈尔滨市| 德昌县| 乌拉特前旗| 芒康县| 湖州市|