C# Array.CreateInstance创建二维数组及如何遍历二维数组
1 static void Main(string[] args) 2 { 3 //创建二维数组 4 int[] lengths = {2,3}; 5 int[] lowerBounds = { 1, 10 }; 6 Array arr1 = Array.CreateInstance(typeof(string),lengths,lowerBounds); 7 //给数组赋值 8 arr1.SetValue("一", 1, 10); 9 arr1.SetValue("二", 1, 11); 10 arr1.SetValue("三", 1, 12); 11 arr1.SetValue("四", 2, 10); 12 arr1.SetValue("五", 2, 11); 13 arr1.SetValue("六", 2, 12); 14 //遍历数组 15 System.Collections.IEnumerator arr2 = arr1.GetEnumerator(); 16 int i = 0; 17 int cols = arr1.GetLength(arr1.Rank - 1); 18 while(arr2.MoveNext()){ 19 if (i < cols) 20 { 21 i++; 22 } 23 else { 24 Console.WriteLine(); 25 i = 1; 26 } 27 Console.Write("\t{0}",arr2.Current); 28 } 29 Console.WriteLine(); 30 Console.ReadKey(); 31 }
站在巨人的肩膀上真的会看的更远更清晰!
-----用志不分,乃凝于神