分类:一维、二维、多维数组
语法:数据类型[ ] 数组名
(1)int[] arr = new int[5]{0,1,2,3,4};
(2)int[] arr = new int[]{0,1,2,3,4}; // 省略长度
(3)int[] arr = {0,1,2,3,4}; // 省略new
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
特点:
(1)定长,不可动态扩展
(2)类型必须与声明相同
对象数组
定义数组元素为对象
例:
public class Student
{
public string name;
public double score;
public void showInfo()
{
Console.WriteLine(name + "\t" + score);
}
}
public class Program
{
static void Main(string[] args)
{
Student[] stus = new Student[2];
stus[0] = new Student();
stus[0].name = "张三";
stus[0].score = 100;
stus[1] = new Student();
stus[1].name = "李四";
stus[1].score = 80;
foreach (Student item in stus)
{
item.showInfo();
}
}
}
输出结果: 张三 100
李四 80