如果我声明这个数组......
string[,] a = {
{"0", "1", "2"},
{"0", "1", "2"},
{"0", "1", "2"},
{"0", "1", "2"},
};
Run Code Online (Sandbox Code Playgroud)
然后我可以测量长度
a.Length
Run Code Online (Sandbox Code Playgroud)
这是12.如何测量阵列的尺寸?如果我试试......
a[0].Length
Run Code Online (Sandbox Code Playgroud)
我得到Wrong number of indices inside []; expected 2.是什么赋予了?
我似乎得到一个奇怪的价值.
如何获取数组中的行数:
double[,] lookup = { {1,2,3}, {4,5,6} };
Run Code Online (Sandbox Code Playgroud)
输出应为2.
下面列出的代码是为了帮助我在C#中找到二维数组的行和列大小,但在访问列长度时(GetLength(1)),我最终会得到一个IndexOutOfRangeException.我的查找类似于q-a,但无法确定列大小.
List<List<int>> intLists = new List<List<int>>();
for (int i = 0; i < 2; i++)
{
List<int> tempList = new List<int>();
for (int j = 0; j < 5; j++)
tempList.Add(j + 5+i);
intLists.Add(tempList);
}
int[][] intArray = intLists.Select(Enumerable.ToArray).ToArray();
Console.WriteLine("Dimension 0 Length = " + intArray[0].Length);
Console.WriteLine("Dimension 1 Length = " + intArray[1].Length);
Console.WriteLine("Dimension 0 Length = " + intArray.GetLength(0));
//Console.WriteLine("Dimension 1 Length = " + intArray.GetLength(1));
Run Code Online (Sandbox Code Playgroud)