c# 中使用foreach和for遍历 一维数组; 二维数组; 交错数组; 敢回答敢采纳

//数字可以随便给!!一维数组;int[]array={"随便举一些例子"}二维数组;int[,]name=newint[4,6]交错数组;int[][]name2=ne... //数字可以随便给!!
一维数组;int[ ] array={"随便举一些例子"}
二维数组; int[ , ] name = new int [ 4,6 ]
交错数组; int [ ] [ ] name2 = new int[ 3 ][ ]
展开
 我来答
mfc198666
推荐于2017-11-23 · TA获得超过142个赞
知道答主
回答量:54
采纳率:0%
帮助的人:51.5万
展开全部
一维数组:不解释你懂的 遍历:
int[] arr = new int[] { 1, 2, 3, 4 };
for (int i = 0; i < arr.Length; i++)
{
MessageBox.Show(arr[i].ToString());
}

多为数组:就是不是一维的数组 - -! 遍历:
int[,] arr1 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
for (int i = 0; i < arr1.GetLength(0); i++)
{
for (int j = 0; j < arr1.GetLength(1); j++)
{
MessageBox.Show(arr1[i, j].ToString());
}
}

交错数组:就是数组的数组 遍历:
int[][] arr2 = new int[][] { new int[] { 4, 3 }, new[] { 2, 1 } };
for (int i = 0; i < arr2.GetLength(0); i++)
{
for (int j = 0; j < arr2[i].Length; j++)
{
MessageBox.Show(arr2[i][j].ToString());
}
}

foreach当然也可以做,但是一般数组还是用for比较好,如果你想要foreach的联系我吧
///===================================
不知道你为什么还不选我,难道你要的三维以上哪种么?我再给你个三维的例子,你可以对比自己改四维

int[, ,] arr1 = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } };

for (int i = 0; i < arr1.GetLength(0); i++)
{
for (int j = 0; j < arr1.GetLength(1); j++)
{
for (int z = 0; z < arr1.GetLength(2); z++)
{
MessageBox.Show(arr1[i, j, z].ToString());
}
}
}

int[][][] arr2 = new int[][][] { new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5 } }, new int[][] { new int[] { 6 }, new int[] { 7, 8 } } };
for (int i = 0; i < arr2.GetLength(0); i++)
{
for (int j = 0; j < arr2[i].Length; j++)
{
for (int z = 0; z < arr2[i][j].Length; z++)
{
MessageBox.Show(arr2[i][j][z].ToString());
}
}
}

每多一维多一层循环就行了。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式