数组:
如果使用变量定义数组大小,该变量必须是一个常量,例如:
const int arraysize = 5;
int myarray = new int[arraysize] {1,2,3,4,5};
foreach循环,下面是一个实例
string[] friendnames = {"robert barwell","mike parry","jeremy beacock"};
Console.WriteLine("here are {0} of my friends:", friendnames.Length);
foreach(string friendname in friendnames)
{
Console.WriteLine(friendname);
}
Console.Read();
//Console.ReadKey();
foreach(数据类型 变量名 in 数组)
这儿的变量名可以不用声明直接用,前面的数据类型必须和后面的数组中的数值类型一致。
foreach循环具有只读功能不能对数组内容进行修改。
定义和访问数组的数组:
jaggedintarray = new int[2][];
jaggedintarray[0] = new int[3];
jaggedintarray[1] = new int[4];
或者采用字面赋值来定义数组的数组
int [][] jaggedintarray = {new int[] {1,2,3}, new int[] {1}; new int[] {1,2}};