猿问

c# 中使用foreach和for遍历 一维数组; 二维数组; 交错数组;

//数字可以随便给!!
一维数组;int[ ] array={"随便举一些例子"}
二维数组; int[ , ] name = new int [ 4,6 ]
交错数组; int [ ] [ ] name2 = new int[ 3 ][ ]

慕侠2389804
浏览 669回答 3
3回答

翻阅古今

//一维数组int[] iArray = new int[] { 1, 2, 3, 45, 6 };foreach (int i in iArray)Console.Write(i.ToString() + ",");Console.WriteLine("\n\n");//2级数组int[,] i2Array = new int[,] { { 1, 2 }, { 3, 4 } };foreach (int i1 in i2Array){Console.Write(i1.ToString() + ",");}Console.WriteLine("\n\n");//交错数组int[][] CrossArray = new int[3][] { new int[]{ 1, 2 }, new int[]{ 3, 4, 5 }, new int[]{ 6, 7, 8, 9 } };foreach (int[] c in CrossArray){foreach (int i in c)Console.Write(i.ToString() + ",");Console.Write("\n");}

慕虎7371278

foreach(var a in array){this.text=a.tostring;}for(int i =0;i<array.upbound;i++){this.text=array[i].tostring;}

牛魔王的故事

static void Main(string[] args){var oneDimensionsArray = new int[] { 1, 2, 3, 4 };var twoDimensionsArray = new int[,] { {1,2},{3,4},{5,6}};var jaggedArray = new int[3][] { new[] { 1 }, new[] { 2, 3 }, new[] { 4, 5, 6 } };Console.WriteLine("遍历一维数组");//遍历一维数组foreach (var i in oneDimensionsArray){Console.WriteLine(i);}Console.WriteLine("遍历二维数组");//遍历二维数组foreach (var i in twoDimensionsArray){Console.WriteLine(i);}Console.WriteLine("遍历交错数组");//遍历交错数组foreach (var arr in jaggedArray){foreach (var i in arr){Console.WriteLine(i);}}}
随时随地看视频慕课网APP
我要回答