数组声明:
int[ ] numble;
创建数组:
int[ ] numble=new int [5];
Animal[ ] zoo=new Animal[100];
Animal是一个类在使用时得先声明;
对类的初始化:
Animal[ ] zoo = new Animal[3]{
new Animal("Wolf"),
new Animal("Lion"),
new Animal("Cat")
};
//数组本身是对象,必须使用new创建数组实例
对数组的声明与初始化:
string[ ] msg = {"Hello","Bye","How are you"};
string s1 = msg[2];
string s2 = msg[3];//在此会导致字符穿的溢出 数租越界;
foreach类似于for,遍历整个数租。foreach自动循环,读出索引从0到lenghth的数组元素
*不需要我们设置循环条件和迭代变量,更简单,更安全
*foreach读出的元素值是只读,不可以修改;
语法:
int[ ] numble = {1,2,3};
foreach (int x in numble ){
Console.writeline(x);
}
数组方法(函数)
Sort 对数租元素进行排序
Clear 将元素设置为默认输出值0或null
Clone 创建数租拷贝
GetLength 获取数组指定维的元素个数
IndexOf 某个值在数组中首次出现的索引