如何通过一个多维数组你循环? [英] how do you loop through a multidimensional array?

查看:151
本文介绍了如何通过一个多维数组你循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

感谢

推荐答案

只需使用两个嵌套循环。要获得尺寸的大小,你可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.array.getlength.aspx"><$c$c>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.

这篇关于如何通过一个多维数组你循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆