一 数组
1一维数组
//统计每一个学生成绩并计算全班10人的平均成绩,然后把所有成绩显示出来
double[] score = new double[10];
double sum = 0;
for (int i = 0; i < 10; i++)
{
Console.WriteLine("请输入学生成绩:");
score[i] = Convert.ToDouble(Console.ReadLine());
sum+=score[i];
}
Console.Clear();//清屏
Console.WriteLine("{0}名学生的平均成绩是{1}",score.Length,sum/score.Length);
Console.ReadKey();
//使用动态数组在不定义数组长度的情况下 统计输入人数的平均分数(动态数组即把数组的定义部分和初始化部分用不同语句表达)
Console.WriteLine("你想录入成绩人数:");
int numbers = Convert.ToInt32(Console.ReadLine());//将string类型转换成int类型,TOInt16()是单精度浮点型,TOInt32()是int型 TOInt64()是double型
double[] score = new double[numbers];
double sum = 0;
for (int i = 0; i < numbers; i++)
{
Console.WriteLine("请输入学生的成绩:");
score[i] = Convert.ToDouble(Console.ReadLine());
sum += score[i];
}
Console.Clear();
Console.WriteLine("{0}名同学的平均成绩是{1}", numbers, sum / score.Length);
Console.ReadKey();
2 二维数组
int[,] arr = new int[2, 3] {{1,2,3},{3,4,5}}; //数组初始化
Console.WriteLine("数组的行数为:{0}",arr.Rank );//arr.Rank代表的是几维数组
Console.WriteLine("数组的列数为:{0}",arr.GetUpperBound(arr.Rank-1)+1); //arr.GetUpperBound()表示该函数返回最大的索引
Console.WriteLine("");
for (int i = 0; i < arr.Rank; i++)
{
string str = "";
for (int j = 0; j < arr.GetUpperBound(arr.Rank-1) + 1; j++)
{
str = str + Convert.ToString(arr[i, j]) + "";
}
Console.WriteLine(str);
}
Console.ReadKey();
3 动态二维数组的声明和使用
Console.WriteLine("请输入动态数组的行数:");
int m = Convert.ToInt32(Console.ReadLine ());
Console.WriteLine("请输入动态数组的列数:");
int n = Convert.ToInt32(Console.ReadLine());
int[,] arr=new int[m,n];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
Console.WriteLine(i+j.ToString());
}
Console.WriteLine();
}
Console.ReadKey();
4 数组的基本操作
1 排序,可分为选择排序,冒泡排序还有通过array类的sort和reverse方法
1)选择排序
int[] array = new int[]{1,23,21,34,233,45,62,35,77};//从大到小排序
for (int i = 0; i < array.Length; i++)
{
Console.Write("{0}",array[i]+" ");
}
for (int i = 0; i <array.Length - 1; i++)
{
int max = i;
for (int j = i+1; j<array.Length;j++)
{
if (array[j] > array[max])
{
max = j;
}
}
int temp = array[max];
array[max] = array[i];
array[i] = temp;
}
Console.WriteLine(" ");
Console.WriteLine("排序后的顺序是:");
foreach (int n in array)
Console.Write("{0}",n+" ");
Console.WriteLine("");
Console.ReadKey();
2)冒泡排序
int[] array = new int[] { 1, 23, 21, 34, 233, 45, 62, 35, 77 };//从大到小排序
for (int i = 0; i < array.Length; i++)
{
Console.Write("{0}",array[i]+" ");
}
for (int i = 0; i < array.Length-1; i++)
{
for (int j = 0; j < array.Length-i-1; j++)
{
if (array[j] >array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
Console.WriteLine(" ");
Console.WriteLine("排序后的顺序是:");
foreach (int n in array)
Console.Write("{0}", n + " ");
Console.WriteLine("");
Console.ReadKey();
3)Array类的sort排序和Reverse排序
注 只能对一维数组排序
int[] array = new int[] { 1, 23, 21, 34, 233, 45, 62, 35, 77 };//从小到大排序
for (int i = 0; i < array.Length; i++)
{
Console.Write("{0}", array[i] + " ");
}
Console.WriteLine(" ");
Array.Sort(array);
Console.WriteLine("排序后结果是:");
foreach (int n in array)
Console.Write("{0}",n+" ");
Console.ReadKey();
int[] array = new int[] { 1, 23, 21, 34, 233, 45, 62, 35, 77 };//反向排序
for (int i = 0; i < array.Length; i++)
{
Console.Write("{0}", array[i] + " ");
}
Console.WriteLine(" ");
Array.Reverse(array);
Console.WriteLine("排序后结果是:");
foreach (int n in array)
Console.Write("{0}", n + " ");
Console.ReadKey();
2 数组的合并与拆分
//一维数组的合并
int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] arr2 = new int[] { 7, 8, 9, 10, 11, 12 };
int n = arr1.Length + arr2.Length;
int[] arr3 = new int[n];
for (int i = 0; i < arr3.Length; i++)
{
if (i < arr1.Length)
arr3[i] = arr1[i];
else
arr3[i]=arr2[i-arr1.Length];
}
Console.WriteLine("合并后的一维数组:");
foreach (int i in arr3)
Console.Write("{0}",i+" ");
Console.ReadKey();
//二维数组的合并
int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] arr2 = new int[] { 7, 8, 9, 10, 11, 12 };
int[,] arr4 = new int[2,6];
for (int i = 0; i < arr4.Rank; i++)
{
switch (i)
{
case 0:
{
for (int j = 0; j < arr1.Length; j++)
arr4[i, j] = arr1[j];
break;
}
case 1:
{
for(int j=0;j<arr2.Length;j++)
arr4[i,j]=arr2[j];
break;
}
}
}
Console.WriteLine("合并后的二维数组:");
for (int i = 0; i < arr4.Rank; i++)
{
for (int j = 0; j < arr4.GetUpperBound(arr4.Rank - 1) + 1; j++)
Console.Write(arr4[i,j]+" ");
Console.WriteLine();
}
Console.ReadKey();
//二维数组的拆分
int[,] arr3 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
int[] arr1= new int[3];
int[] arr2=new int[3];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
switch (i)
{
case 0:arr1[j]=arr3[i,j];break;
case 1: arr2[j] = arr3[i, j]; break;
}
}
}
Console.WriteLine("数组一:");
foreach (int n in arr1)
Console.Write(n+" ");
Console.WriteLine();
Console.WriteLine("数组二:");
foreach (int n in arr2)
Console.Write(n + " ");
Console.ReadKey ();