交錯數組(C# 編程指南)

交錯數組是元素爲數組的數組。 交錯數組元素的維度和大小能夠不一樣。 交錯數組有時稱爲「數組的數組」。如下示例說明如何聲明、初始化和訪問交錯數組。編程

下面聲明一個由三個元素組成的一維數組,其中每一個元素都是一個一維整數數組:數組

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

必須初始化 jaggedArray 的元素後才能夠使用它。 能夠以下例所示初始化該元素:spa

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

每一個元素都是一個一維整數數組。 第一個元素是由 5 個整數組成的數組,第二個是由 4 個整數組成的數組,而第三個是由 2 個整數組成的數組。.net

也能夠使用初始值設定項用值填充數組元素,在這種狀況下不須要數組大小。 例如:debug

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

還能夠在聲明數組時將其初始化,如:code

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

能夠使用下面的速記格式。 請注意:不能從元素初始化中省略 new 運算符,由於不存在元素的默認初始化:blog

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

交錯數組是數組的數組,所以其元素是引用類型並初始化爲 nullip

能夠以下例所示訪問個別數組元素:element

 
    
 
    
// 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;
// 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;
 
    
 
    
 
    
 
    
 
    
 
    
 
    
 
    

能夠混合使用交錯數組和多維數組。 下面聲明和初始化一個一維交錯數組,該數組包含大小不一樣的三個二維數組元素。 有關二維數組的詳細信息,請參閱 多維數組(C# 編程指南)get

 
    
 
    
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} } 
};
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} } 
};
 
    
 
    
 
    
 
    
 
    
 
    
 
    
 
    

能夠如本例所示訪問個別元素,該示例顯示第一個數組的元素 [1,0] 的值(值爲 5):

 
    
 
    
System.Console.Write("{0}", jaggedArray4[0][1, 0]);
System.Console.Write("{0}", jaggedArray4[0][1, 0]);
 
    
 
    
 
    
 
    
 
    
 
    
 
    
 
    

方法 Length 返回包含在交錯數組中的數組的數目。 例如,假定您已聲明瞭前一個數組,則此行:

 
    
 
    
System.Console.WriteLine(jaggedArray4.Length);
System.Console.WriteLine(jaggedArray4.Length);
 
    
 
    
 
    
 
    
 
    
 
    
 
    
 
    

返回值 3。

本例生成一個數組,該數組的元素爲數組自身。 每個數組元素都有不一樣的大小。

 
     
 
     
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();
    }
}/* Output: Element(0): 1 3 5 7 9 Element(1): 2 4 6 8 */
相關文章
相關標籤/搜索