如何通过2维阵列FOREACH? [英] How to foreach through a 2 dimensional array?
本文介绍了如何通过2维阵列FOREACH?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个二维数组。
If I've got a 2 dimensional array.
string[,] table = {
{ "aa", "aaa" },
{ "bb", "bbb" }
};
和我想通过它的foreach这样。
And I'd like to foreach through it like this.
foreach (string[] row in table)
{
Console.WriteLine(row[0] + " " + row[1]);
}
不过,我得到的错误无法字符串类型转换为字符串[]
But, I get the error "Can't convert type string to string[]
有没有一种方法可以让我达到我想要的,即通过与迭代变量数组的第一个维度遍历我返回该行的1维数组?
Is there a way I can achieve what I want, i.e. iterate through the first dimension of the array with the iterator variable returning me the 1 dimensional array for that row?
感谢。
推荐答案
如果你定义阵列是这样的:
If you define your array like this:
string[][] table = new string[][] { new string[] { "aa", "aaa" }, new string[]{ "bb", "bbb" } };
然后你就可以在其上使用foreach循环。
Then you can use a foreach loop on it.
这篇关于如何通过2维阵列FOREACH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文