您的位置:首页 > 编程语言 > C#

交错数组(C# 编程指南)

2016-01-03 19:37 579 查看
交错数组是元素为数组的数组。 交错数组元素的维度和大小可以不同。
交错数组有时称为“数组的数组”。以下示例说明如何声明、初始化和访问交错数组。

下面声明一个由三个元素组成的一维数组,其中每个元素都是一个一维整数数组:





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


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


















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





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 个整数组成的数组。

也可以使用初始值设定项用值填充数组元素,在这种情况下不需要数组大小。
例如:





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


















还可以在声明数组时将其初始化,如:





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 运算符,因为不存在元素的默认初始化:





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


















交错数组是数组的数组,因此其元素是引用类型并初始化为 null。

可以如下例所示访问个别数组元素:





// 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# 编程指南)。





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
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: