欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

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

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

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

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

必須初始化 jaggedArray 的元素后才可以使用它??梢匀缦吕境跏蓟撛兀?/p>

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 運算符,因為不存在元素的默認初始化:

  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ù)組的詳細信息,請參閱多維數(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ù)組。
在查詢表達式中,隱式類型的數(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 [] 進行初始化。
對象初始值設(shè)定項中的隱式類型的數(shù)組
創(chuàng)建包含數(shù)組的匿名類型時,必須在該類型的對象初始值設(shè)定項中對數(shù)組進行隱式類型化。在下面的示例中,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" }
    }
};


網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有