2012-08-30 31 views
1

我有一个非常愚蠢的问题。请原谅它晚了,我累了。 :)获取2d阵列中二维数组的数量

我有定义整数的2D阵列如下:

int[,] myArray = new int[,] // array of 7 int[,] arrays 
{ 
    { 1, 10 }, // 0 
    { 2, 20 }, // 1 
    { 3, 30 }, // 2 
    { 4, 40 }, // 3 
    { 5, 50 }, // 4 
    { 6, 60 }, // 5 
    { 7, 70 }, // 6     
}; 

正如可以看到阵列由7 INT [,]阵列。

当我打电话myArray.Length它的结果是14.我需要的是7.如何获得int [,]数组的数量?什么是调用方法(我期待的结果是7)。

再次感谢!

回答

2

不是二维数组的数组 - 它是一个二维数组。如前所述,尺寸由myArray.GetLength(dimension)给出。它不是一个具有“7 int [,]数组”的数组 - 它只是一个7乘2的数组。

如果要数组的数组(实际上,载体的载体中),它是:

int[][] myArray = { 
    new int[] {1,10}, // alternative: new[]{1,10} - the "int" is optional 
    new int[] {2,20}, 
    new int[] {3,30}, 
    new int[] {4,40}, 
    new int[] {5,50}, 
    new int[] {6,60}, 
    new int[] {7,70}, 
}; 

然后7myArray.Length是。

+0

非常感谢! –

+0

我喜欢这种方式!我想我会用你的建议看起来更优雅! :) –

5

使用GetLength方法来获得一维的长度。

myArray.GetLength(0) 

尝试下面几行:

Console.WriteLine(myArray.GetLength(0)); 
Console.WriteLine(myArray.GetLength(1)); 

你会得到

7 
2 
+0

啊!这就是谢谢:) –

+0

@JanTacci,不客气 – Habib