c# 二维数组遍历 小弟是搞java的 最近闲 看看c#才看到二维数组 就把我难住了。。很囧问题是如何遍历二维数组比如 int[,] ary = new int[3,4];如何用for循环遍历呢?又如何用foreach遍历?不能用i<3当作条件。。orz 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 应该是for(int i=0;i<3;i++) for(int j=0;j<4;j++) {}foreach int i in ary {} 这两种方法都能遍历啊int i = 0, j = 0;for(i=0;i<3;i++){ for(j=0;j<4,j++) { MessageBox.Show(ary[i,j].ToString()); }}foreach(int num in ary){ MessageBox.Show(num.ToString());} 不用i<3做条件可以用i<ary.GetLength(0) //取第0维的长度相对应的j<ary.GetLength(1) string s = null; int[,] ary = new int[3, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 0, 11, 12 } }; for (int i = 0; i < ary.GetLength(0); i++) { for (int j = 0; j < ary.GetLength(1); j++) { s+= Convert.ToString(ary[i, j]); return s; } } //foreach(int ARow in ary) //{ // Console.WriteLine("{0}\t", ARow); //} int count = 0; foreach(int i in arr2) { Console.Write(i+"\t"); count++; if(count % arr2.GetLength(1) == 0) { Console.WriteLine(); } } >>>>老问题,关于图片显示刷新闪烁问题??? C# word翻页? c#打印问题 ,高手指点 关于C#的一个菜鸟问题。 数据连接及全局变量的问题! 这句代码出错,请问为什么?Diagnostics.Process.GetCurrentProcess.ProcessName.ToString 钳套使用 SqlDataReader 后连接池没有关闭???怎样才能关闭! 在C#中,如何访问父类的构造函数 关于消息发送,在线等待!!! 关于数组的值移位的问题 Queue队列线程同步问题。 在webbrowser里面打开网页的瞬间如何用progressbar来标是进度
for(int i=0;i<3;i++)
for(int j=0;j<4;j++)
{}foreach int i in ary
{}
for(i=0;i<3;i++)
{
for(j=0;j<4,j++)
{
MessageBox.Show(ary[i,j].ToString());
}
}foreach(int num in ary)
{
MessageBox.Show(num.ToString());
}
可以用i<ary.GetLength(0) //取第0维的长度
相对应的j<ary.GetLength(1)
string s = null;
int[,] ary = new int[3, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 0, 11, 12 } };
for (int i = 0; i < ary.GetLength(0); i++)
{
for (int j = 0; j < ary.GetLength(1); j++)
{ s+= Convert.ToString(ary[i, j]);
return s;
}
}
//{
// Console.WriteLine("{0}\t", ARow);
//}
foreach(int i in arr2)
{
Console.Write(i+"\t");
count++;
if(count % arr2.GetLength(1) == 0)
{
Console.WriteLine();
}
}