第1个回答 2015-04-20
楼主看看我的 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace How_to_use_Array { class Program { static void Main() { /***************************** 一维数组 *******************************/ //int[] myint = new int[4] { 1, 2, 3, 4 }; /*声明方式一*/ //int[] myint; //myint = new int[4] { 1, 2, 3, 4 }; /*声明方式二*/ //int[] myint = { 1, 2, 3, 4 }; /*声明方式三*/ //for (int i = 0; i < myint.Length; i++) /*遍历方法一*/ //{ // Console.WriteLine(myint[i]); //} //foreach (int i in myint) /*遍历方法二*/ //{ // Console.WriteLine(i); //} /******************************* 多维数组 ******************************/ //int[, ] myint = new int[,] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 0, 11, 12 } }; /*声明方式一*/ //int[,] myint; //myint = new int[,] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 0, 11, 12 } }; /*声明方式二*/ //int[,] myint = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 0, 11, 12 } }; /*声明方式三*/ //Console.WriteLine("befor: {0}\n", myint[1, 2]); //myint[1, 2] = 100; /*可改变数组指定元素的值*/ //Console.WriteLine("after: {0}\n", myint[1, 2]); //for (int i = 0; i < myint.Rank; i++) //{ // for (int j = 0; j < 4; j++) // { // Console.WriteLine(myint[i, j]); // } //} /*遍历方法一*/ //foreach (int i in myint) //{ // Console.WriteLine(i); //} /*遍历方法二*/ /***************************** 交错数组(数组的数组) *******************************/ //int[][] myint = new int[3][]; //myint[0] = new int[5] { 1, 2, 3, 4, 5 }; //myint[1] = new int[3] { 4, 5, 6 }; //myint[2] = new int[2] { 7, 8 }; /*声明方式一*/ int[][] myint = new int[3][] { new int[5] { 1, 2, 3, 4, 5 }, new int[3] { 4, 5, 6 }, new int[2] { 7, 8 } }; /*声明方式二*/ //for (int i = 0; i < myint.Length; i++) //{ // for (int j = 0; j < myint[i].Length; j++) // { // Console.WriteLine(myint[i][j]); // } //} /*遍历方法一*/ foreach (int[] myint2 in myint) { foreach (int myInt in myint2) { Console.WriteLine(myInt); } } /*遍历方法二*/ Console.Read(); } } }本回答被提问者采纳