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

深入解析C#中的交錯數(shù)組與隱式類型的數(shù)組

 更新時間:2016年01月28日 15:30:59   投稿:goldensun  
這篇文章主要介紹了深入解析C#中的交錯數(shù)組與隱式類型的數(shù)組,隱式類型的數(shù)組通常與匿名類型以及對象初始值設(shè)定項和集合初始值設(shè)定項一起使用,需要的朋友可以參考下

交錯數(shù)組
交錯數(shù)組是元素為數(shù)組的數(shù)組。交錯數(shù)組元素的維度和大小可以不同。交錯數(shù)組有時稱為“數(shù)組的數(shù)組”。以下示例說明如何聲明、初始化和訪問交錯數(shù)組。
下面聲明一個由三個元素組成的一維數(shù)組,其中每個元素都是一個一維整數(shù)數(shù)組:

int[][] jaggedArray = new int[3][];

必須初始化 jaggedArray 的元素后才可以使用它。可以如下例所示初始化該元素:

jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];

每個元素都是一個一維整數(shù)數(shù)組。第一個元素是由 5 個整數(shù)組成的數(shù)組,第二個是由 4 個整數(shù)組成的數(shù)組,而第三個是由 2 個整數(shù)組成的數(shù)組。
也可以使用初始值設(shè)定項用值填充數(shù)組元素,在這種情況下不需要數(shù)組大小。例如:

jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };

還可以在聲明數(shù)組時將其初始化,如:

  int[][] jaggedArray2 = new int[][] 
{
  new int[] {1,3,5,7,9},
  new int[] {0,2,4,6},
  new int[] {11,22}
};

可以使用下面的速記格式。請注意:不能從元素初始化中省略 new 運算符,因為不存在元素的默認(rèn)初始化:

  int[][] jaggedArray3 = 
{
  new int[] {1,3,5,7,9},
  new int[] {0,2,4,6},
  new int[] {11,22}
};

交錯數(shù)組是數(shù)組的數(shù)組,因此其元素是引用類型并初始化為 null。
可以如下例所示訪問個別數(shù)組元素:

// Assign 77 to the second element ([1]) of the first array ([0]):
jaggedArray3[0][1] = 77;

// Assign 88 to the second element ([1]) of the third array ([2]):
jaggedArray3[2][1] = 88;

可以混合使用交錯數(shù)組和多維數(shù)組。下面聲明和初始化一個一維交錯數(shù)組,該數(shù)組包含大小不同的三個二維數(shù)組元素。有關(guān)二維數(shù)組的詳細(xì)信息,請參閱多維數(shù)組(C# 編程指南)。

int[][,] jaggedArray4 = new int[3][,] 
{
  new int[,] { {1,3}, {5,7} },
  new int[,] { {0,2}, {4,6}, {8,10} },
  new int[,] { {11,22}, {99,88}, {0,9} } 
};

可以如本例所示訪問個別元素,該示例顯示第一個數(shù)組的元素 [1,0] 的值(值為 5):

System.Console.Write("{0}", jaggedArray4[0][1, 0]);
方法 Length 返回包含在交錯數(shù)組中的數(shù)組的數(shù)目。例如,假定您已聲明了前一個數(shù)組,則此行:

System.Console.WriteLine(jaggedArray4.Length);
返回值 3。
本例生成一個數(shù)組,該數(shù)組的元素為數(shù)組自身。每一個數(shù)組元素都有不同的大小。

class ArrayTest
{
  static void Main()
  {
    // Declare the array of two elements:
    int[][] arr = new int[2][];

    // Initialize the elements:
    arr[0] = new int[5] { 1, 3, 5, 7, 9 };
    arr[1] = new int[4] { 2, 4, 6, 8 };

    // Display the array elements:
    for (int i = 0; i < arr.Length; i++)
    {
      System.Console.Write("Element({0}): ", i);

      for (int j = 0; j < arr[i].Length; j++)
      {
        System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");
      }
      System.Console.WriteLine();      
    }
    // Keep the console window open in debug mode.
    System.Console.WriteLine("Press any key to exit.");
    System.Console.ReadKey();
  }
}

輸出:

  Element(0): 1 3 5 7 9
  Element(1): 2 4 6 8


隱式類型的數(shù)組
可以創(chuàng)建隱式類型的數(shù)組,在這樣的數(shù)組中,數(shù)組實例的類型是從數(shù)組初始值設(shè)定項中指定的元素推斷而來的。有關(guān)任何隱式類型變量的規(guī)則也適用于隱式類型的數(shù)組。
在查詢表達(dá)式中,隱式類型的數(shù)組通常與匿名類型以及對象初始值設(shè)定項和集合初始值設(shè)定項一起使用。
下面的示例演示如何創(chuàng)建隱式類型的數(shù)組:

class ImplicitlyTypedArraySample
{
  static void Main()
  {
    var a = new[] { 1, 10, 100, 1000 }; // int[]
    var b = new[] { "hello", null, "world" }; // string[]

    // single-dimension jagged array
    var c = new[]  
{ 
  new[]{1,2,3,4},
  new[]{5,6,7,8}
};

    // jagged array of strings
    var d = new[]  
{
  new[]{"Luca", "Mads", "Luke", "Dinesh"},
  new[]{"Karen", "Suma", "Frances"}
};
  }
}

請注意,在上一個示例中,沒有在初始化語句的左側(cè)對隱式類型的數(shù)組使用方括號。另請注意,交錯數(shù)組就像一維數(shù)組那樣使用 new [] 進(jìn)行初始化。
對象初始值設(shè)定項中的隱式類型的數(shù)組
創(chuàng)建包含數(shù)組的匿名類型時,必須在該類型的對象初始值設(shè)定項中對數(shù)組進(jìn)行隱式類型化。在下面的示例中,contacts 是一個隱式類型的匿名類型數(shù)組,其中每個匿名類型都包含一個名為 PhoneNumbers 的數(shù)組。請注意,對象初始值設(shè)定項內(nèi)部未使用 var 關(guān)鍵字。

    var contacts = new[] 
{
  new {
      Name = " Eugene Zabokritski",
      PhoneNumbers = new[] { "206-555-0108", "425-555-0001" }
    },
  new {
      Name = " Hanying Feng",
      PhoneNumbers = new[] { "650-555-0199" }
    }
};


相關(guān)文章

最新評論

遂昌县| 大安市| 永吉县| 托克逊县| 惠水县| 甘孜| 松滋市| 航空| 双城市| 得荣县| 乌兰浩特市| 松溪县| 民和| 黄骅市| 泰州市| 江华| 山东| 杭州市| 荣昌县| 景谷| 崇文区| 三穗县| 九龙城区| 汝城县| 龙里县| 天门市| 云阳县| 河曲县| 铁岭市| 绥化市| 吉木乃县| 高淳县| 襄汾县| 宜春市| 神木县| 育儿| 郁南县| 荣昌县| 德阳市| 阿尔山市| 中江县|