C#数组和元组

声明数组

int[] myArray;

初始化数组

myArray = new int[4];

数组是引用类型当初始化完毕后,将在托管堆上分配内存空间,其结构图如下

C#数组和元组

声明和初始化放在一起

int[] myArray = new int[4]

int[] myArray = new int[4] {4, 7, 11, 2};

int[] myArray = new int[] {4, 7, 11, 2};

int[] myArray = {4, 7, 11, 2};

引用类型数组

下面自定义一个person类

public class person

{

  public string firstname{get;set;}

  public string lastname{get;set;}

  public override string ToString()//重写了基类的string类

  {

    return string.fromat("{0},{1}",firstname,lastname);
  }

}

现在定义一个person数组

Person[] myPersons = new Person[2];

对每一个元素进行初始化

myPersons[0] = new Person { FirstName="Ayrton", LastName="Senna" };
myPersons[1] = new Person { FirstName="Michael", LastName="Schumacher" };

也可以这样定义

Person[] myPersons2 =
{
new Person { FirstName="Ayrton", LastName="Senna"},
new Person { FirstName="Michael", LastName="Schumacher"}
};

该数组在内存中的存储结构

C#数组和元组

多维数组

二维数组

int[,] twodim = new int[3, 3];
twodim[0, 0] = 1

定义兼赋值

int[,] twodim = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

三维数组

int[,,] threedim = {
{ { 1, 2 }, { 3, 4 } },
{ { 5, 6 }, { 7, 8 } },
{ { 9, 10 }, { 11, 12 } }
};

锯齿数组

int[][] jagged = new int[3][];
jagged[0] = new int[2] { 1, 2 };
jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 };
jagged[2] = new int[3] { 9, 10, 11 };

数组类 array class

array是抽象类型不能够new

Array intArray1 = Array.CreateInstance(typeof(int), 5);
for (int i = 0; i < 5; i++)
{
intArray1.SetValue(33, i);
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(intArray1.GetValue(i));
}

也可以把该数组转换为传统c#定义的int数组

int[] intArray2 = (int[])intArray1;

createInstance有好多重载函数

创二维数组

int[] lengths = { 2, 3 };
            int[] lowerBounds = { 1, 10 };
            Array array2 = Array.CreateInstance(typeof(Person), lengths, lowerBounds);
            array2.SetValue(new Person
            {
                FirstName = "tian",
                LastName = "hello"
            },1,10);

也可以对此数组进行转换

Person[,] racers2 = (Person[,])racers;
Person first = racers2[1, 10];
Person last = racers2[2, 12];

数组的复制

因为数组是引用类型,把数组的名称直接赋给另一个数组其实就是把引用赋给了这个数组他们还是指向同一个数组

如果数组的元素类型是值类型可以通过下面方法进行复制

int[] intArray1 = {1, 2};
int[] intArray2 = (int[])intArray1.Clone();

这两个数组在内存中的存储结构如下

C#数组和元组

如果数组包括引用类型也只是引用的复制,他们指向的还是同一个对象

Person[] beatles = {
new Person { FirstName="John", LastName="Lennon" },
new Person { FirstName="Paul", LastName="McCartney" }
};
Person[] beatlesClone = (Person[])beatles.Clone();

其中person是自定义的类类型

其在内存中的存储结构如下

C#数组和元组

clone方法是数组的浅复制,如果想要对包括引用类型的数组进行deep copy,那么就必须遍历数组然后对数组每一项进行创建新对象

数组的比较

public class Person : IComparable<Person>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public override string ToString()
        {
            return String.Format("{0} {1}", FirstName, LastName);
        }

public int CompareTo(Person other)
        {
            if (other == null) return 1;
            int result = string.Compare(this.FirstName, other.FirstName);
            if (result == 0)
            {
                result = string.Compare(this.LastName, other.LastName);
            }
            return result;
        }
    }

被比较数组的元素必须要实现icomparable接口

static void Main(string[] args)
        {
            Person[] persons =
            {
                new Person{FirstName = "Damon",LastName = "Hill"},
                new Person{FirstName = "Niki",LastName = "Lauda"},
                new Person{FirstName = "Ayrton" ,LastName ="Senna"},
               new Person{FirstName = "Graham",LastName = "Hill"}
            };
            Array.Sort(persons);
            foreach(var pr in persons)
            {
                Console.WriteLine(pr);
            }
        }

 元组(tuples)

数组包含是相同类型的元素而元组可以包含不同类型的元素

static void Main(string[] args)
        {
            var result = Divide(5, 2);
            Console.WriteLine("result of division:{0},remender:{1}", result.Item1, result.Item2);
            var tuple = Tuple.Create<string, string, string, int, int, int,double, Tuple<int, int>>("tian", "mo", "chou", 4, 5, 6, 2.3,Tuple.Create<int, int>(20, 20));
            ;
        }
        public static Tuple<int,int> Divide(int dividend,int divisor)
        {
            int result = dividend / divisor;
            int reminder = dividend % divisor;
            return Tuple.Create<int, int>(result, reminder);
        }

上一篇:XML语法小结


下一篇:51nod 1076强连通