2017.08.08-C#-一维数组

#region数组

//相同数据类型的成员组成的一组数据;目的方便管理;

//申明方法,使用数组前,必须进行初始化赋值;

//int[] numbers = {1,2,3,4,5};

float[]scores;

string[]names;

//初始化数组:动态初始化,静态初始化;

//动态初始化:数组名=new类型[数组长度];

int[] numbers= new int[10];//默认值0;

scores= new float[10];//默认值0.0f;

names= new string[10];//默认值null(空对象);

int[] numbers_1 = new int[3]{1,2,3};

int[] numbers_2 = new int[]{1,2,3};

string[] names_1 = new string[]{"China","English"};

//静态初始化

int[] numbers_3 = {1,2,3,4,5,6,7,8,9,0};

string[] names_2 = {"Andy","Ethan"};

//通过数组下标访问数组中的成员

stringname = names_2[0];

//Console.WriteLine(name);

//避免下标越界

//stringname_1 = names_2[3];

//inta = 3;

//if(a < names_2.Length) {

//Console.WriteLine(names_2[a]);

//}

//

//数组的遍历

//for(int i = 0; i < names_2.Length; i++) {

//Console.WriteLine(names_2[i]);

//}

//修改数组成员

//numbers_3[5]= 7;

//

//for(int i = 0; i < numbers_3.Length; i++) {

//Console.WriteLine(numbers_3[i]);

//}

//Exercise

//反向打印数组所有成员

//求数组中所有元素和

//int[]intArray = {1,12,34,2,5,6};

//

//for(int i = intArray.Length-1; i >= 0; i--) {

//

//Console.WriteLine(intArray[i]);

//}

//intsum = 0;

//

//for(int i = 0; i < intArray.Length; i++) {

//sum+=intArray[i];

//}

//Console.WriteLine(sum);

#endregion

t

你可能感兴趣的:(2017.08.08-C#-一维数组)