• IComparable与IComparer接口在集合类中的用处


    在简单的类型中,我们可以使用CompareTo,与Compare进行比较

    例如:

    在简单类型中使用
    1 int intA = 4;
    2  int intB =5;
    3 int intResult = intA.CompareTo(intB);
    4 Console.WriteLine(intResult);
    5 intResult=System.Collections.Comparer.Default.Compare(a,b);
    6 Console.WriteLine(intResult);

    那么在我们的自定义类型中怎么做呢?

    我们通过VS的对象管理器可以知道,我们的CompareTo方法与Compare方法都是IComparable接口和IComparer接口提供的。

    如果我们自定义类中实现了这两个接口,那么也可以做这种操作!!!

    实现IComparable接口的例子:

      

    实现IComparable接口
    1 class Student : IComparable
    2 {
    3 public int age;
    4 public string name;
    5 public Student()
    6 {
    7 }
    8 public Student(string name, int age)
    9 {
    10 this.age = age;
    11 this.name = name;
    12 }
    13 public int CompareTo(object obj)
    14 {
    15 if (obj is Student)
    16 {
    17 Student stu = (Student)obj;
    18 return this.age - stu.age;
    19 }
    20 else
    21 {
    22 throw new Exception("类型不兼容!");
    23 }
    24 }
    25 }
    26 class Program
    27 {
    28 static void Main(string[] args)
    29 {
    30 Student stu = new Student("秦涛",23);
    31 Student stu1 = new Student("王涛",35);
    32 if (stu.CompareTo(stu1) > 0)
    33 {
    34 Console.WriteLine(stu.name + "年龄大于" + stu1.name + "的年龄");
    35 }
    36 else if (stu.CompareTo(stu1) == 0)
    37 {
    38 Console.WriteLine(stu.name + "年龄等于" + stu1.name + "的年龄");
    39 }
    40 else
    41 {
    42 Console.WriteLine(stu.name + "年龄小于" + stu1.name + "的年龄");
    43 }
    44 }
    45 }

    实现IComparer接口的例子:

    实现IComparer接口
    1 class StudentDefault:System.Collections.IComparer
    2 {
    3 public static StudentDefault Default = new StudentDefault();
    4 public int Compare(object x, object y)
    5 {
    6 if (x is Student && y is Student)
    7 {
    8 Student s1 = (Student)x;
    9 Student s2 = (Student)y;
    10 return s1.age - s2.age;
    11 }
    12 else
    13 {
    14 throw new Exception("类型不兼容");
    15 }
    16 }
    17
    18 }
    19 class Program
    20 {
    21 static void Main(string[] args)
    22 {
    23 Student stu = new Student("秦涛",23);
    24 Student stu1 = new Student("王涛",35);
    25 int intResult= StudentDefault.Default.Compare(stu, stu1);
    26 if (intResult > 0)
    27 {
    28 Console.WriteLine(stu.name + "年龄大于" + stu1.name + "的年龄");
    29 }
    30 else if (intResult == 0)
    31 {
    32 Console.WriteLine(stu.name + "年龄等于" + stu1.name + "的年龄");
    33 }
    34 else
    35 {
    36 Console.WriteLine(stu.name + "年龄小于" + stu1.name + "的年龄");
    37 }
    38 }
    39 }

    有上面的例子可以得出结论如下:

       IComparable在要比较的对象的类中实现,可以比较该对象和另一个对象
       IComparer在一个单独的类中实现,可以比较任意两个对象。

    class Student : IComparable
    {
    public int age;
    public string name;
    public Student()
    {
    }
    public Student(string name, int age)
    {
    this.age = age;
    this.name = name;
    }
    public int CompareTo(object obj)
    {
    if (obj is Student)
    {
    Student stu
    = (Student)obj;
    return this.age - stu.age;
    }
    else
    {
    throw new Exception("类型不兼容!");
    }
    }
    }
    class StudentDefault:System.Collections.IComparer
    {
    public static StudentDefault Default = new StudentDefault();
    public int Compare(object x, object y)
    {
    if (x is Student && y is Student)
    {
    Student s1
    = (Student)x;
    Student s2
    = (Student)y;
    return s1.age - s2.age;
    }
    else
    {
    throw new Exception("类型不兼容");
    }
    }

    }
    class Program
    {
    static void Main(string[] args)
    {
    Student stu
    = new Student("秦涛",23);
    Student stu1
    = new Student("王涛",35);
    Student stu2
    = new Student("张军", 28);
    ArrayList list
    = new ArrayList();
    list.Add(stu);
    list.Add(stu1);
    list.Add(stu2);
    foreach (Student var in list)
    {
    Console.WriteLine(var.name
    + "年龄是" + var.age);
    }
    list.Sort();
    foreach (Student var in list)
    {
    Console.WriteLine(var.name
    + "年龄是" + var.age);
    }
    list.Sort(StudentDefault.Default);
    foreach (Student var in list)
    {
    Console.WriteLine(var.name
    + "年龄是" + var.age);
    }
    }
    }
    怀揣着一点点梦想的年轻人
    相信技术和创新的力量
    喜欢快速反应的工作节奏
  • 相关阅读:
    商人的诀窍
    商人小鑫
    懒虫小鑫
    悼念512汶川大地震遇难同胞
    最少拦截系统
    活动选择问题
    删数问题
    常用排序算法的时间和空间复杂度总结
    一些博主
    C中自己的memcpy
  • 原文地址:https://www.cnblogs.com/hfliyi/p/2014992.html
Copyright © 2020-2023  润新知