另一个数组 C# 中的数组
Array in another Array C#
我在 C# 中创建代码,但无法在另一个中创建数组。
int[, , ,] linee = new int[4, 4, 4, 4];
int[] line1 = new int[] { 10, 50, 150, 50 };
int[] line2 = new int[] { 10, 50, 10, 100 };
int[] line3 = new int[] { 10, 100, 150, 100 };
int[] line4 = new int[] { 150, 50, 150, 100 };
linee[0] = line1;
它给我错误:
Error CS0022 The number of indexes in [] is incorrect.
(在最后一行)
对于 C# 中的多维数组,您需要这样的东西
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
您正在混淆 multi-dimensional arrays with jagged arrays 或数组的数组。
您正在声明一个多维数组,但随后您尝试将其赋值,就好像它是一个锯齿状数组一样。
对于多维数组,您需要像这样单独分配值:
int[,,,] linee = new int[4, 4, 4, 4];
linee[0, 0, 0, 0] = 10;
linee[0, 0, 0, 1] = 50;
linee[0, 0, 0, 2] = 150;
linee[0, 0, 0, 3] = 50;
对于锯齿状数组,您可以按照您尝试的方式分配现有数组:
int[][][][] jaggedArray = new int[4][][][];
int[] line1 = new int[] { 10, 50, 150, 50 };
jaggedArray[0][0][0] = line1;
我在 C# 中创建代码,但无法在另一个中创建数组。
int[, , ,] linee = new int[4, 4, 4, 4];
int[] line1 = new int[] { 10, 50, 150, 50 };
int[] line2 = new int[] { 10, 50, 10, 100 };
int[] line3 = new int[] { 10, 100, 150, 100 };
int[] line4 = new int[] { 150, 50, 150, 100 };
linee[0] = line1;
它给我错误:
Error CS0022 The number of indexes in [] is incorrect.
(在最后一行)
对于 C# 中的多维数组,您需要这样的东西
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
您正在混淆 multi-dimensional arrays with jagged arrays 或数组的数组。
您正在声明一个多维数组,但随后您尝试将其赋值,就好像它是一个锯齿状数组一样。
对于多维数组,您需要像这样单独分配值:
int[,,,] linee = new int[4, 4, 4, 4];
linee[0, 0, 0, 0] = 10;
linee[0, 0, 0, 1] = 50;
linee[0, 0, 0, 2] = 150;
linee[0, 0, 0, 3] = 50;
对于锯齿状数组,您可以按照您尝试的方式分配现有数组:
int[][][][] jaggedArray = new int[4][][][];
int[] line1 = new int[] { 10, 50, 150, 50 };
jaggedArray[0][0][0] = line1;