C#学习 数组(22)
创建数组
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
int[] myNum1 = {10, 20, 30, 40};
int[] myNum2 = new int[4]{10, 20, 30, 40};
int[] myNum3 = new int[]{10, 20, 30, 40};
int[] myNum4 = new int[4];
myNum4[0]=10;
myNum4[1]=20;
myNum4[2]=30;
myNum4[3]=40;
访问: 索引从0开始
myNum4[0]; // 10
循环
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.Length; i++)
{
Console.WriteLine(cars[i]);
}
foreach(string s in cars){
Console.WriteLine(s);
}
排序
Array.Sort(cars);
函数
myNum4.Max(); // 最大值
myNum4.Min(); // 最小值
myNum4.Sum(); // 求和
myNum4.Average(); // 平均值
创建多维数组
int[,] myNum5 =
{
{ 1, 2, 3, 4 },
{ 10, 20, 30, 40 },
};
访问多维数组元素
myNum5[0,0]; // 1
myNum5[1,3]; // 40
本文来自博客园,作者:huiy_小溪,转载请注明原文链接:https://www.cnblogs.com/huiy/p/18545484