foreach可以用于遍历二维数组吗?

using System;
class ArrayTest
{
public static void Main()
{
char[] name = new char[4] { 'M', 'i', 'k', 'e' };
foreach (char a in name)
{
Console.Write("{0}", a);
}
Console.WriteLine();

}
}
这样是对的
但我想用foreach遍历二维数组,代码如下:
using System;
class ArrayTest
{
public static void Main()
{
char[][] name = new char[3][];//声明二维数组name,其中包含3个数组,而它们的元素数可能不同
name[0] = new char[4] { 'M', 'i', 'k', 'e' };
name[1] = new char[5] { 'S', 'o', 'n', 'i', 'a' };
name[2]=new char[3]{'T','o','m'};
foreach (char[] x in name)
{
Console.Write("\n");
Console.Write("{0}", x);
}

}
}
出现错误,要怎么改呢?
谢谢!
我用的是C#,搞定了,代码如下,谢谢你
using System;
class ArrayTest
{
public static void Main()
{

char[][] name = new char[3][];
name[0] = new char[4] { 'M', 'i', 'k', 'e' };
name[1] = new char[5] { 'S', 'o', 'n', 'i', 'a' };
name[2]=new char[3]{'T','o','m'};
foreach (char[] x in name)
{
Console.Write("\n");
foreach (char b in x)
{
Console.Write("{0}", b);
}
}

}
}

第1个回答  2008-10-31
可以,像这种形式。
foreach($array as $key=>$value)
{
foreach($value as $skey=>$svalue)
{
echo $svalue;//$svalue 就是你要的二维数组结果。
}
}
没有时间给你检查,你去试试。
第2个回答  2012-09-07
嵌套下就可以了本回答被提问者采纳
第3个回答  2020-05-07
嵌套下了
相似回答