1.灵魂三问
why (为什么)
解决大批量数据计算问题
有10万个变量:其中3万个整数,4万个双精度,2万个字符串,1万个对象
整数数组:3万个整数
双精度数组:4万个双精度
字符串数组:2万个字符串
对象数组:1万个对象
what(什么)
动态数组(显示数组):数据类型[] 变量名=new 数据类型[大小];
int[] a=new int[30000];
when(当)
你什么时候去谁用数组当需要使用到多组数列的时候就可以去使用数组
2.静态数组和动态数组
静态数组是在声明时已经确定子数组大小的数组,即数组元素的个数固定不变。声明数组,就是声明数组名、维数、类型、数组大小。
动态数组,是相对于静态数组而言。静态数组的长度是预先定义好的,在整个程序中,一旦给定大小后就无法改变。而动态数组则不然,它可以随程序需要而重新指定大小。
3.访问
数组的访问是:a[i] i的下标(从0开始)
赋值:a[0]=1;
int []a=new int [5]; 五个值的长度 但是是从0-4 不是1-5
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
a[4]=5;
Console.Write(a[0]+"\t"+a[3]);
void Get2()
{
int[] a = new int[5];
//for (int i = 0; i <5; i++)
//for (int i = 0; i <= 4; i++)
for (int i = 0; i <a.Length; i++)//a.Length:数组的长度
{
a[i] = i + 1;
}
Console.WriteLine(a[0] + "\t" + a[3]);
// 未经处理的异常: System.IndexOutOfRangeException: 索引超出了数组界限。
static void Main(string[] args)
{
Test15 t = new Test15();
t.Get2();
静态数组(隐式数组)
int[] x={1,2,3,4,5};
void Get3()
{
//把a数组的值倒序放入b数组
int[] a = { 1, 2, 3, 4, 5 };
int[] b = new int[5];
for (int i = a.Length-1; i >= 0; i--)//循环赋值
{
b[4-i] = a[i];//i:4 3 2 1 0
}
for (int i = 0; i < b.Length; i++)//循环输出
{
Console.WriteLine(b[i]);
}
}
4.排序
Array.Sort()
//把数据正序排列
int[] a = { 95, 74, 63, 87, 55 };
Array.Sort(a);
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
//把数据倒序排列
for (int i = a.Length-1; i >=0 ; i--)
{
Console.WriteLine(a[i]);
}
void Get5()
{
//冒泡排序
int[] a = { 95, 74, 63, 87, 55 };
int t;
for (int i = 0; i < a.Length; i++)
{
for (int j = i; j < a.Length; j++)
{
if (a[i] > a[j])//由小到大
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
}
void Insert()//插入排序
{
int[] a = { 99, 85, 82, 63, 60,0 };
int b=65,index=0;
for (int i = 0; i < a.Length; i++)
{
if (b >= a[i])
{
index = i;
break;
}
}
//99,85,82,63, 60 []
//99,85,82,[i] ,63, 60
//99,85,82,[index] ,63(i), 60
for (int i = a.Length - 1; i >= index + 1; i--)
{
a[i] = a[i - 1];
}
a[index] = b;
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
}
//选择排序
static void SelctSort(params int[] arry) {
for (int i = 0; i < arry.Length - 1; i++)
{
int min = arry[i];
int minindex = i;
for (int j = i + 1; j < arry.Length; j++)
{
if (arry[j] < min)
{
min = arry[j];
minindex = j;
}
}
int temp = arry[i];
arry[i] = arry[minindex];
arry[minindex] = temp;
}
show(arry);
}
//快速排序
static void QuickSort(params int[]arry) {
A(arry, 0, arry.Length - 1);
show(arry);
}
static void A(int[] arry, int left, int right) {
if (left<right)
{
int i = left;
int j = right;
int x = arry[left];
while (i<j)
{
while (i<j)
{
if (arry[j]<x)
{
arry[i] = arry[j];
break;
}
else
{
j--;
}
}
while (i<j)
{
if (arry[i]>x)
{
arry[j] = arry[i];
break;
}
else
{
i++;
}
}
arry[i] = x;
A(arry, left, i - 1);
A(arry, i + 1, right);
}
}
}
5.求数列中的最大值最小值
Random r = new Random();
int[] s = new int[10];
int max = int.MinValue;
int min = int.MaxValue;
for (int i = 0; i < s.Length; i++)
{
int a = r.Next(10, 50);
s[i] = a;
if (s[i] > max)
{
max = s[i];
}
if (s[i] < min)
{
min = s[i];
}
Console.WriteLine(s[i]);
}
Console.WriteLine();
Console.WriteLine(min);
Console.WriteLine(max);