如何通过一个多维数组你循环?多维、数组

2023-09-03 16:45:38 作者:独奏我的歌、

foreach (String s in arrayOfMessages)
{
System.Console.WriteLine(s);
}

字符串[,] arrayOfMessages并正在作为参数传递。

String[,] arrayOfMessages and is being passed as a parameter.

我希望能够确定哪些是从arrayOfMessages [0,i]和arrayOfMessages [N,I] 其中n是阵列的最后索引

I want to be able to determine which ones are from arrayOfMessages[0,i] and arrayOfMessages[n, i] where n is the final index of the array.

感谢

推荐答案

只需使用两个嵌套循环。要获得尺寸的大小,你可以使用GetLength():

Simply use two nested for loops. To get the sizes of the dimensions, you can use GetLength():

for (int i = 0; i < arrayOfMessages.GetLength(0); i++)
{
    for (int j = 0; j < arrayOfMessages.GetLength(1); j++)
    {
        string s = arrayOfMessages[i, j];
        Console.WriteLine(s);
    }
}

此假设你确实有字符串[,] 。在.NET中它也可能有不是从0收录在这种情况下,多维数组,它们必须重新presented为在C#中,你阵需要使用 GetLowerBound() GetUpperBound()中得到的边界为每个维度。

This assumes you actually have string[,]. In .Net it's also possible to have multidimensional arrays that aren't indexed from 0. In that case, they have to be represented as Array in C# and you would need to use GetLowerBound() and GetUpperBound() the get the bounds for each dimension.