数组:连续存储相同数据类型的数据类型。
1.声明数组
数据类型 [] 数组名;
int [] score ;
2.初始化数组
score = new int[5] {1,2,2,3,4};
3.数组读写操作
score[0] = 5;
Console.Writeline( score[0] );
注意事项:
1.防止访问位置超出数组的长度(数组名.Length)
例程1.
static void Main()
{
string[] friendNames = { "Robert Barwell", "Mike Parry","Jeremy Beacock" }; //定义字符串数组
int i;
Console.WriteLine("Here are {0} of my friends:", friendNames.Length); //获取数组长度
for (i = 0; i < friendNames.Length; i++)
{
Console.WriteLine(friendNames[i]);
}
Console.ReadKey();
}
以上都是针对一维数组的操作,下面简单介绍一下二维数组。
double[,] score = new double[3,4]; //定义一个三行四列的二维数组
其中行为第0行到第2行,第0列到第3列。
具体的数据处理和一维数组一样。