数组
数组可以分为一维数组、二维数组、三维数组以及多维数组。
数组隐式继承于System.Array抽象类,而System.Array又继承于System.Object,所以数组是引用类型。
初始化一维数组:string[] TestArray=new string[30]
初始化二维数组:string[,] TestArrary=new string[2,3]{{"a","b","c"},{"d","e","f"}}
class Program { static void Main(string[] args) { string[,] TestArray = new string[2, 3] { { "a","b","c"}, {"d","e","f" } }; TestArray[0, 0] = "aaa"; string name=TestArray[0,0]; Console.WriteLine(TestArray[0,0]); string[,] Name = new string[4, 5]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { Name[i, j] ="["+i.ToString()+","+j.ToString()+"]"; } } for (int i = 0; i <4; i++) { for (int j = 0; j <5; j++) { Console.Write(Name[i,j]); } Console.WriteLine(); } Console.ReadLine(); } }
简单的二维数组售票系统
class Program { static void Main(string[] args) { string[,] SaleTicket=new string[5,6]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 6; j++) { SaleTicket[i, j] = "【 空 】"; } } Console.WriteLine("请输入要选择的座位号格式为(0,1),输入Q退出"); string Input = Console.ReadLine(); while(Input!="q") { string[] strNew = Input.Split(','); int One = int.Parse(strNew[0]); int Two = int.Parse(strNew[1]); SaleTicket[One, Two] = "【 已售】"; System.Console.Clear(); for (int i = 0; i <5; i++) { for (int j = 0; j < 6; j++) { Console.Write(SaleTicket[i,j]); } Console.WriteLine(); } Console.WriteLine("请输入要选择的座位号格式为(0,1),输入Q退出"); Input = Console.ReadLine(); } } }