最基本数组
数组的定义:存储相同数据类型的集合
定义一个长度为4的整形数组,这个数组只可以村整形数据
第一种写法:动态分配内存空间
int [] intArray=new int[4];
第二种写法
int[] intArray=new int{}[1,2,3,4] ;
注意:初始化数组的时候,要么给定长度,要么初始化数组内元素个数
第三种写法
int[] intArray={1,2,3,4};
实际上我们int[]它的原形为
而int数组可以用foreach循环的原因归根结底为,Array类本身已经实现了 IEnuMrable这个接口
所以int[]才可以使用foreach
访问数组的元素
intArray[0];//访问数组第0个位置存储的元素
Console.WriteLine ("数组第0个位置存储的数据为:"+intArray[0]);
//访问数组第4个位置存储的元素-----访问越界(程序崩溃)
Console.WriteLine ("数组第4个位置存储的数据为:"+intArray[4]);
遍历数组
for (int i = 0; i < intArray.Length; i++) {
Console.WriteLine ("下标:{0},值为:(0)",i,intArray[i]);
}
遍历数组2
foreach (int item in intArray) {
Console.WriteLine ("数组元素为:"+item);
}
如何修改数组中的元素呢
通过索引取值进行赋值
intArray[2]=10.0f;
intArray [2] = 10;
遍历数组2
foreach (int item in intArray) {
Console.WriteLine ("数组元素为:"+item);
}
练习1:分别声明string,char,float,double相应类型的数组,进行遍历访问
string[] a = { "a","b","c" };
for (int i = 0; i < a.Length; i++) {
Console.WriteLine (a[i]);
}
char[] a = { 'a','b','c','d' };
for (int i = 0; i < a.Length; i++) {
Console.WriteLine (a[i]);
}
float[] a = { 1f,2f,3f,4f };
for (int i = 0; i < a.Length; i++) {
Console.WriteLine (a[i]);
}
double[] a = { 0.1,0.2,0.3,0.4 };
for (int i = 0; i < a.Length; i++) {
Console.WriteLine (a[i]);
}
对象类型数组
Person[] person = new Person[] {
new Person ("小伟", 18),
new Person ("小刘", 28),
};
foreach (Person p in person) {
Console.WriteLine ("姓名,年龄",p.name,p.age);
}
Array arr = Array.CreateInstance (typeof(int), 5);
arr.SetValue (1,0);
arr.SetValue (2,1);
arr.SetValue (3,2);
for (int i = 0; i < 5; i++) {
Console.WriteLine (arr.GetValue(i));
}
把arr数组转换为int【】数组
int [] newIntArray=(int[])arr;
foreach (int item in newIntArray) {
Console.WriteLine (item);
}
练习2:分别定义练习1类型对应的Array数组,设置数组元素,并遍历
string[] a = { "a","b","c" };
char[] b = { 'a','b','c','d' };
float[] c = { 1f,2,3,4 };
double[] d. = { 0.1,0.2,0.3,0.4 };
Array a = Array.CreateInstance (typeof(string), 3);
a.SetValue ("a", 0);
a.SetValue ("b", 1);
a.SetValue ("c", 2);
for (int i = 0; i < a.Length; i++) {
Console.WriteLine (a.GetValue(i));
}
Array b = Array.CreateInstance (typeof(char), 3);
b.SetValue ('a', 0);
b.SetValue ('b', 1);
for (int i = 0; i < b.Length; i++) {
Console.WriteLine (b.GetValue(i));
}
Array c = Array.CreateInstance (typeof(float), 3);
c.SetValue (1f, 0);
c.SetValue (2f, 1);
c.SetValue (3f, 2);
for (int i = 0; i < c.Length; i++) {
Console.WriteLine (c.GetValue (i));
}
Array d = Array.CreateInstance (typeof(double), 3);
d.SetValue (0.1, 0);
d.SetValue (0.2, 1);
d.SetValue (0.3, 2);
for (int i = 0; i < d.Length; i++) {
Console.WriteLine (d.GetValue(i));
}
ArraySegment<T>
int [] intArray1={1,2,3,4,};
int[] intArray2 = { 5, 6, 7, 8 };
var se1 = new ArraySegment<int>[2] {
new ArraySegment<int> (intArray1, 0, 3),
new ArraySegment<int> (intArray2, 3, 1)
};
Print (se1);
练习3
找出数组中最大值以及最大值的位置
int i=0;
int index=0;
int[] arr= {3,2,5,6,8};
int max = arr [0];
for ( i = 0; i <arr.Length; i++) {
if (max<arr[i]) {
max = arr [i];
index = i;
}
}
Console.WriteLine ( "最大值为:{0}最大值位置:{1}",max,index);
练习4:定义一个整数数组,初始化3个整形元素,打印出最大值,最小值以及平均值
int[] a={4,6,8};
int sv = 0;
int max=a[0];
int min = a[0];
for (int i = 0; i < a.Length; i++) {
if (max <= a [i]) {
max = a [i];
}else {
min = a[i];
}
sv = ((a [0] + a [1] + a [2]) / 3);
}
Console.WriteLine ("最大值:{0}最小值:{1}平均值:{2}",max,min,sv);
练习5:定义一个数组,从键盘上获取三个整数数值,打印出最大值
int[] a=new int[3];
for (int i = 0; i < a.Length; i++) {
a[i]=int.Parse(Console.ReadLine());
}
Console.WriteLine ((a[0]>a[1]?a[0]:a[1])>a[2]?(a[0]>a[1]?a[0]:a[1]):a[2]);
练习6:拼接俩个字符串数组,到一个数组,遍历显示
int [] intArray1={3,2,9,4,8};
int[] intArray2 = { 5, 6, 7, 8 };
var se1 = new ArraySegment<int>[2] {
new ArraySegment<int> (intArray1, 0, 3),
new ArraySegment<int> (intArray2, 1, 1)
};
Print (se1);
}
static void Print(ArraySegment<int>[] segements)
{
foreach (var segment in segements) {
for (int i =segment.Offset; i < segment.Offset+segment.Count; i++) {
Console.WriteLine (segment.Array[i]);
}
}
}
//********************************************************************************************
}
}