• C#学习之自定义数组及其排序


    在C#中对数组的定义比较灵活。这里着重说一下自定义数组和Array类的排序。

    在Array类中通过属性Length就可以获取整个数组中数据的数量,可以通过foreach迭代数组。

    使用Rank属性可以获取数组的维数,通过属性LongLength也可获取数组中数据的数量,但是基本上不用。

    它是当数组中放置的数据量超出了整数的范围时才用。

    Array类中排序的方法比较简单,对于string 和 Int 类型直接用Array.Sort()就可以。

    但是对于自定义的数组就需要在类中写出Array.Sort()中使用的排序方法。

    如下:

     1 namespace ArrayStudy
     2 {
     3     //类需要实现IComparable<>接口
     4     public class Person:IComparable<Person>
     5     {
     6         public string FirstName { get; set; }
     7         public string LastName { get; set; }
     8         public override string ToString()
     9         {
    10             return string.Format("{0} {1}",FirstName,LastName);
    11         }
    12         //CompareTo是IComparable接口里面的函数
    13         //如果相比较的两个相等,结果是0
    14         //Array.sort()会根据返回的值进行排序
    15         public int CompareTo(Person other)
    16         {
    17             if(other==null)
    18             {
    19                 throw new ArgumentNullException("other");
    20             }
    21             int result = this.FirstName.CompareTo(other.FirstName);
    22             if(result==0)
    23             {
    24                 result = this.LastName.CompareTo(other.LastName);
    25             }
    26             return result;
    27         }
    28     }
    29 }

    上面的类中,是对FirstName 排序的,如果FirstName相同,就比较LastName.

    主函数调用方式如下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ArrayStudy
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Person[] person ={
    14                                  new Person{FirstName="Chen",LastName="Long"},
    15             new Person{FirstName="Wang",LastName="Xinyu"},
    16             new Person{FirstName="Jia",LastName="Yankun"},
    17             new Person{FirstName="Jiao",LastName="Yuanyuan"},
    18             new Person{FirstName="Zhang",LastName="Huangjian"}
    19                              };
    20 
    21 
    22             Array.Sort(person);
    23             foreach(Person p in person)
    24             {
    25                 Console.WriteLine(p.ToString());
    26             }
    27             Console.WriteLine("Rank:{0}",person.Rank);
    28             Console.WriteLine("LongLength:{0}",person.LongLength);
    29             Console.WriteLine("Length:{0}",person.Length);
    30             Console.ReadKey();
    31             return;
    32         }
    33     }
    34 }

     而对于不能确定要对哪一个变量进行排序的时候就需要增加一个新的比较类。这个类是实现了IComparer<>接口.

    在这个接口里面含有int Compare(T x,T y)比较函数,里面含有两个变量。

     1 namespace ArrayStudy
     2 {
     3     //首先定义枚举的类型,在下面的switch中进行判断
     4     public enum PersonCompareType
     5     {
     6         FirstName,
     7         LastName
     8     }
     9     public class PersonComparer:IComparer<Person>
    10     {
    11         private PersonCompareType comparetype;
    12         public PersonComparer(PersonCompareType         comparetype)
    13         {
    14             this.comparetype = comparetype;
    15         }
    16         public int Compare(Person x,Person y)
    17         {
    18             if (x == null) throw new ArgumentNullException("x");
    19             if (y == null) throw new ArgumentNullException("y");
    20             int result = x.FirstName.CompareTo(y.FirstName);
    21             int result2= x.LastName.CompareTo(y.LastName);
    22             switch(comparetype)
    23             {
    24 //如果对目标类型排序发现相同,就对另外一个排序并返回值
    25                 case PersonCompareType.FirstName:
    26                     {
    27                         if (result == 0)
    28                             return result2;
    29                         return result;
    30                     }
    31                 case PersonCompareType.LastName:
    32                     {
    33                         if (result2 == 0)
    34                             return result;
    35                         return result2;
    36                     }
    37                 default :
    38                     throw new ArgumentException(
    39                         "unexpected compare type"
    40                     );
    41             }
    42         }
    43     }
    44 }

    将主函数中的排序改成如下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ArrayStudy
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Person[] person ={
    14                                  new Person{FirstName="Chen",LastName="Long"},
    15             new Person{FirstName="Wang",LastName="Xinyu"},
    16             new Person{FirstName="Jia",LastName="Yankun"},
    17             new Person{FirstName="Jiao",LastName="Yuanyuan"},
    18             new Person{FirstName="Zhang",LastName="Huangjian"}
    19                              };
    20 
    21     //在Sort()后面添加第二个参数,这个参数是比较的方法类型
    22             Array.Sort(person,new PersonComparer(PersonCompareType.FirstName));
    23             foreach(Person p in person)
    24             {
    25                 Console.WriteLine(p.ToString());
    26             }
    27             Console.WriteLine("Rank:{0}",person.Rank);
    28             Console.WriteLine("LongLength:{0}",person.LongLength);
    29             Console.WriteLine("Length:{0}",person.Length);
    30             Console.ReadKey();
    31             return;
    32         }
    33     }
    34 }

    这样便可以灵活选择排序的对象了。

    What I don't dare to say is I can't!
  • 相关阅读:
    【转】【SEE】基于SSE指令集的程序设计简介
    【转】【Asp.Net】asp.net服务器控件创建
    ControlTemplate in WPF ——ScrollBar
    ControlTemplate in WPF —— Menu
    ControlTemplate in WPF —— Expander
    ControlTemplate in WPF —— TreeView
    ControlTemplate in WPF —— ListBox
    ControlTemplate in WPF —— ComboBox
    ControlTemplate in WPF —— TextBox
    ControlTemplate in WPF —— RadioButton
  • 原文地址:https://www.cnblogs.com/sytu/p/3984720.html
Copyright © 2020-2023  润新知