一维数组,多维数组~~~~~
Number1 数组、
定义一个数组:
int[]aaa=new string[3]{111,222,333};
这是一个简单的一维数组,其有以下特点:1,数组内长度固定 2,数组内数据类型相同
string[] aaa = new string[] { "aa", "b" }; foreach (string s in aaa) { Console.WriteLine(s); } Console.ReadLine(); //显示 // aa // b
或者可以用以下方式定义:
1,int[]aaa=new int[2];
aaa[0]=222; aaa[1]=333;
其中2代表数组的长度是2。 0代表数组的索引号,0是第一个数据。 1同样也是数组索引号,1是第二个数据。
变量类型 [] 数组名称=new int[数组长度];
aaa[索引号]=值;
2,int []aaa=new int[]{222,333};
变量类型 [] 数组名称=new string[]{值,值,值};
3,二维数组
int []aaa=new int[2,2]{{2,2},{3,3}};
这是一个二维数组,[2,2]中第一个2代表有两个一维数组,第二个2表示每个一维数组中有两个变量。
变量类型 [] 数组名 =new int [有几个一维数组,一维数组中有几个变量] {{值,值},{值,值}};
4,三维数组(多维数组)
int[]aaa=new int[2,2,2]{{{2,2},{3,3}},{{2,2},{3,3}}};
这是一个三维数组,[2,2,2]中第一个2表示有两个二维数组,第二个2表示每个二维数组中有两个一维数组,每个一维数组中有两个变量。
变量类型[]数组名=new int[有几个二维数组,每个二维数组中有几个一维数组,一维数组中有几个变量]{{{值,值},{值,}},{{值,值},{值,值}}};
冒泡排序:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 测试 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 int[] zu1 = new int[] { 5, 2, 3, 4, 1 }; 13 int a; 14 for (int z = 0; z < 5; z++) 15 { 16 for (int x = z + 1; x < 5; x++) 17 { 18 if (zu1[z] < zu1[x]) 19 { 20 a = zu1[z]; 21 zu1[z] = zu1[x]; 22 zu1[x] = a; 23 } 24 } 25 26 } 27 for (int x = 0; x < 5; x++) 28 { 29 Console.WriteLine(zu1[x]); 30 } 31 Console.ReadKey(); 32 33 } 34 } 35 }
对5,2,3,4,1进行从大到小排序。
第一个for循环是选出要进行比较大小的值。第二个for循环是选出要和第一个值比较大小的值。if语句中是如果第一个值小于第二个值,把第一个值和第二个值的位置调换。
第三个for循环是依次输出从大到小的值。