C#高级语法基础知识总结3——数组

一维数组

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

 

二维数组

int[,] arraty=new int[2,2], int[,] arraty=new int[2,2]{{1,2},{2,3}}

 

锯齿数组

            Console.WriteLine("锯齿数组的历遍");

            int[][] array1 = new int[2][];

            array1[0] = new int[2] { 1, 2 };

            array1[1] = new int[3] { 3, 4, 5 };

            for (int row = 0; row < array1.Length; row++)

            {

                for (int j = 0; j < array1[row].Length; j++)

                {

                    Console.WriteLine("array[{0}][{1}]={2}", row, j, array1[row][j]);

                }

            }

 

Array类

Array类是一个抽象类,所以不能使用构造函数创建数组。

创建数组

 1             Array intArray1 = Array.CreateInstance(typeof(int), 5);//CreateInstance()创建Array;类数组
 2 
 3             for (int j = 0; j < 5; j++)
 4 
 5             {
 6 
 7                 intArray1.SetValue(33, j);//SetValue(,index)赋值
 8 
 9             }
10 
11             for (int j = 0; j < 5;j++ )
12 
13             {
14 
15                 Console.WriteLine(intArray1.GetValue(j));//GetValue(index)取值
16 
17             }

 

CreateInstance()方法的重载

创建一个包含2*3个元素的二维数组。第一维基于1,第二维基于10.

1             int[] lengths = { 2, 3 };
2 
3             int[] lowerBounds = { 1, 10 };
4 
5             Array racers = Array.CreateInstance(typeof(int), lengths, lowerBounds);
6 
7             racers.SetValue(new person { FirstName = "国强", LastName = "" }, 1, 10);
8 
9             racers.SetValue(new person { FirstName = "国强", LastName = "Soar" }, 1, 11);

 

复制数组Clone()和Copy(),Clone()方法会创建一个新数组,而Copy()方法必须传递阶数相同且有足够元素的已有数组。

1             person[] beatles = { new person { FirstName = "国强", LastName = "" }, new person { FirstName = "国强", LastName = "Soar" } };
2 
3             person[] beatlesClone = (person[])beatles.Clone();

 

使用Icomparable接口实现对姓式的排序

 

Icomparable接口实现排序
 1             person[] person1 ={
 2 
 3                                  new person{FirstName="国强",LastName=""},
 4 
 5                                  new person{FirstName="锦涛",LastName=""},
 6 
 7                                  new person{FirstName="家宝",LastName=""},
 8 
 9                              };
10 
11             Array.Sort(person1);
12 
13             for (int j = 0; j < person1.Length;j++ )
14 
15             {
16 
17                 Console.WriteLine("{0}{1}",person1[j].LastName , person1[j].FirstName);
18 
19             }
20 
21  
22 
23        public class person:IComparable<person>
24 
25         {
26 
27             public string FirstName { get; set; }
28 
29             public string LastName { get; set; }
30 
31  
32 
33             public int CompareTo(person other)
34 
35             {
36 
37                 if (other == null) throw new ArgumentNullException("other");
38 
39                 int result = this.LastName.CompareTo(other.LastName);
40 
41                 if (result == 0)
42 
43                 {
44 
45                     result = this.FirstName.CompareTo(other.FirstName);
46 
47                 }
48 
49                 return result;
50 
51             }
52 
53         }

 

 

posted @ 2012-07-16 22:21  Ghost Soar  阅读(396)  评论(0编辑  收藏  举报