[英]Jagged array with two 2d array
How to create a jagged array that consists of two 2d array? 如何创建一个由两个2d数组组成的锯齿状数组? please help. 请帮忙。 Thank you. 谢谢。
int[][] jaggedArray = new int[3][];
the above code creates a single-dimensional array that has three elements, each of which is a single-dimensional array of integers. 上面的代码创建了一个包含三个元素的单维数组,每个元素都是一个整数的数组。 Can any one help me in creating a two 2d array. 任何人都可以帮我创建一个两个2d数组。
What about this: 那这个呢:
int[][,] jaggedArray = new int[3][,];
The ,
creates the 2D array in the jagged array. ,
在锯齿状数组中创建2D数组。 Read more on Multi-dimensional arrays on MSDN . 阅读MSDN上的多维数组。
Next, you have to initialize every 2D array inside that array: 接下来,您必须初始化该数组中的每个2D数组:
int[,] 2dArray1 = new int[2,3];
jaggedArray[0] = 2dArray1;
int[,] 2dArray2 = new int[4,5];
jaggedArray[1] = 2dArray2;
And so on. 等等。
I think you want something like this, 我想你想要这样的东西,
var jaggedArray = new[]
{
new[] { 1 },
new[] { 1, 2 ,3 },
new[] { 1, 2 }
};
this creates a "jagged" array, with two dimensions where each "row" has a different length. 这会创建一个“锯齿状”数组,其中包含两个维度,其中每个“行”具有不同的长度。
All of the following assertions would be True
. 所有以下断言都是True
。
jaggedArray.Length == 3
jaggedArray[0].Length == 1
jaggedArray[1].Length == 3
jaggedArray[2].Length == 2
If you knew the lengths were fixed but, didn't know the data, you could do, 如果你知道长度是固定的,但是,不知道数据,你可以这样做,
var jaggedArray = new[] { new int[1], new int[3], new int[2] };
Following on from you comment, maybe you want something like this, 继续你评论,也许你想要这样的东西,
var jaggedArray1 = new[]
{
new[] { 1, 2, 3, 4 },
new[] { 1, 2, 3 },
new[] { 1, 2 }
};
var jaggedArray2 = new[]
{
new[] { 1, 2, 3 },
new[] { 1, 2, 3, 4 }
};
int[][][] jaggedArray = new[]
{
jaggedArray1,
jaggedArray2
};
you could just do, 你可以这样做,
var jaggedArray = new[]
{
new[]
{
new[] { 1, 2, 3, 4 },
new[] { 1, 2, 3 },
new[] { 1, 2 }
},
new[]
{
new[] { 1, 2, 3 },
new[] { 1, 2, 3, 4 }
}
};
The second pair of brackets indicates the dimensions. 第二对括号表示尺寸。 So it's like you are declaring a multi-dimensional array except you don't need to specify dimensions in the definition.You can initialize each array with different dimensions. 所以它就像你声明一个多维数组,除了你不需要在定义中指定维度。你可以用不同的维度初始化每个数组。
int[][,] jaggedArray = new int[3][,];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.