c#中的数组

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. namespace CSharp基础
  7. {
  8.     class 数组示例
  9.     {
  10.         public static void Main()
  11.         { 
  12.             //二维数组
  13.             int[,] arr1 = { {12,12},{12,12}};
  14.             //锯齿数组
  15.             int[][] arr2 = new int [3][] ;
  16.             arr2[0] = new int[] { 12,12};
  17.             arr2[1] = new int[] { 12, 12 };
  18.             arr2[2] = new int[] { 12, 12 };
  19.             //其实int[]在编译背后实则是一个Array类的子类,可以调用array中的方法,也可以显示的与array转换
  20.             Array arr3 = Array.CreateInstance( typeof(int) , 3 );
  21.             foreachint i in arr3 )
  22.             {
  23.                 arr3.SetValue(22,i) ;
  24.             }
  25.             //CreateInstance的重载方法可以创建多维数组
  26.             //数组的维数 2*3
  27.             int[] lengths = { 3, 3 };
  28.             //数组的一维与二维的起始下标
  29.             int[] lowerBounds = { 1 , 11 };
  30.             Array arr4 = Array.CreateInstance( typeof(int) ,lengths , lowerBounds );
  31.             arr4.SetValue(555 , 1 ,11 );
  32.             arr4.SetValue(555, 1 , 12);
  33.             arr4.SetValue(555, 1, 13);
  34.             arr4.SetValue(555, 2, 11);
  35.             arr4.SetValue(555, 2, 12);
  36.             arr4.SetValue(555, 2, 13);
  37.             arr4.SetValue(555, 3, 11);
  38.             arr4.SetValue(555, 3, 12);
  39.             arr4.SetValue(555, 3, 13);
  40.             //复制数组 1 使用ICloneable, Array类是实现了ICloneable的,调用clone方法将实现浅拷贝
  41.             int[,] arr5 = (int[,])arr4.Clone();
  42.             foreach (int i in arr5)
  43.             {
  44.                 Console.WriteLine(i);
  45.             }
  46.             //浅拷贝实现方法2
  47.             int[,] arr6 = new int[3,3];
  48.             Array.Copy(arr4, arr6, arr4.Length);
  49.             foreach (int i in arr6)
  50.             {
  51.                 Console.WriteLine(i);
  52.             }
  53.             //如果想实现深拷贝,必须跌代数组,创建新对象
  54.             //数组的排序,c#中要求数组中的元素实现了IComparable接口才可以排序,string int等都是实现过的
  55.             string[] names = { "zhao" , "aladdin" , "emep""microsoft"};
  56.             Array.Sort(names) ;
  57.             foreachstring str in names )
  58.             {
  59.                 Console.WriteLine(str);
  60.             }
  61.             //自定义类型实现排序
  62.             Person[] pers = new Person[] { new Person("aladdin", 8), new Person("zhao", 12), new Person("emep", 44), new Person("ms", 2) };
  63.             Array.Sort( pers );
  64.             foreach( Person per in pers )
  65.             {
  66.                 Console.WriteLine( "姓名{0} 年纪:{1}",per.name ,per.age );
  67.             }
  68.             //以下示例,很明显,是一个引用数组的拷贝,只是浅拷贝,copyPers[0]完全可以改变原对象的引用,这不是我们希望的
  69.             Person[] copyPers = (Person[])pers.Clone();
  70.             copyPers[0].name = "eidt ms";
  71.             Console.WriteLine( pers[0].name );
  72.             //要实现深度拷贝,是一个耗费资源的事,需要跌代,创建
  73.             Person[] copyPers2 = new Person[pers.Length];
  74.             for (int i = 0; i < pers.Length; i++)
  75.             {
  76.                 copyPers2[i] = new Person( pers[i].name ,pers[i].age );
  77.             }
  78.             copyPers2[1].name = "eidt zhao";
  79.             Console.WriteLine(pers[1].name);
  80.             //从上面结果可以看出,堆中的数据也产生了副本,实现深度拷贝
  81.             //用IComparer接品实现排序
  82.             Array.Sort( copyPers2, new PersionComparer() );
  83.             Console.WriteLine("------------按名称字母排序----------------------");
  84.             foreach (Person per in copyPers2)
  85.             {
  86.                 Console.WriteLine("姓名{0} 年纪:{1}", per.name, per.age);
  87.             }
  88.             
  89.             Console.ReadLine();
  90.             
  91.         }
  92.         class PersionComparer : IComparer
  93.         {
  94.             public int Compare( object x , object y )
  95.             {
  96.                 Person p1 = x as Person;
  97.                 Person p2 = y as Person;
  98.                 if (p1 != null && p2 != null)
  99.                 {
  100.                     return p1.name.CompareTo( p2.name );
  101.                 }
  102.                 else
  103.                 {
  104.                     return -1;
  105.                 }
  106.             }
  107.         }
  108.         class Person : IComparable
  109.         {
  110.             public string name;
  111.             public int age;
  112.             public Person(string name, int age)
  113.             {
  114.                 this.name = name ;
  115.                 string a = "sd";
  116.                 this.age = age ;
  117.             }
  118.             //按年纪做比较
  119.             public int CompareTo(object obj)
  120.             {
  121.                 Person per = obj as Person ;
  122.                 if (per != null)
  123.                 {
  124.                     if (this.age == per.age)
  125.                     {
  126.                         return 0;
  127.                     }
  128.                     else if (this.age > per.age)
  129.                     {
  130.                         return 1;
  131.                     }
  132.                     else
  133.                     {
  134.                         return -1;
  135.                     }
  136.                 }
  137.                 return -1;                
  138.             }
  139.         }
  140.     }
  141. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值