zoukankan      html  css  js  c++  java
  • 委托和泛型方法实现获得任意类型数组中的最大值

    委托方式实现:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 求任意数组的最大值
    {
        class Program
        {
            delegate int CompareDelegate(object o1,object o2);
    
            static void Main(string[] args)
            {
                object[] array = new object[] {33,22,55,77,11};
                object max = GetMax(array,CompareInt);
                Console.WriteLine(max);
    
                object maxStr = GetMax(new object[] { "c", "bbbb", "aa" }, CompareString);
                Console.WriteLine(maxStr);
    
                Console.ReadKey();
            }
    
            static int CompareInt(object o1, object o2)
            {
                int n1 = Convert.ToInt32(o1);
                int n2 = Convert.ToInt32(o2);
    
                return n1 - n2;
            }
    
            static int CompareString(object s1,object s2)
            {
                string str1 = s1.ToString();
                string str2 = s2.ToString();
    
                return str1.Length - str2.Length;
            }
    
            static Object GetMax(object[] array,CompareDelegate del)
            {
                object max = array[0];
                for (int i = 1; i < array.Length; i++)
                {
                    if (del(max,array[i])<0)
                    {
                        max = array[i];
                    }
                }
                return max;
            }
    
        }
    }

    泛型方法实现:(推荐)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace _02泛型方法实现任意数组的最大值
    {
        delegate int GetMaxDeletate<T>(T t1, T t2);
    
        class Program
        {
            static void Main(string[] args)
            {
                int maxInt = GetMax<int>(new int[] { 3, 55, 11, 77, 333, 27 }, CompareInt);
                Console.WriteLine(maxInt);
    
                Person[] persons = { 
                                new Person(){Name="张三",Age =18},
                                new Person(){Name= "李四",Age= 55}
                                };
    
                Person maxPerson = GetMax<Person>(persons, ComparePerson);
                Console.WriteLine(maxPerson.Name);
    
                Console.ReadKey();
            }
    
            //比较数组的大小
            static int CompareInt(int i1, int i2)
            {
                return i1 - i2;
            }
    
            //比较Person对象的大小
            static int ComparePerson(Person p1, Person p2)
            {
                return p1.Age - p2.Age;
            }
    
            //T 起到占坑的作用 约束数据的类型
            static T GetMax<T>(T[] array, GetMaxDeletate<T> del)
            {
                T max = array[0];
    
                for (int i = 1; i < array.Length; i++)
                {
                    if (del(max, array[i]) < 0)
                    {
                        max = array[i];
                    }
                }
                return max;
            }
        }
    
        class Person
        {
            public string Name { get; set; }
    
            public int Age { get; set; }
    
        }
    }
  • 相关阅读:
    杯具,丢失了一部分邮件
    Android Building System 总结
    build/envsetup.sh
    PhoneApp是什么时候被创建的
    测试电信的WAP PUSH的方法
    修改Activity响应音量控制键修改的音频流
    ril崩溃时的出错地址定位
    java interface 强制类型转换小记
    android 修改系统程序图标大小
    git 合并patch的方法
  • 原文地址:https://www.cnblogs.com/zxx193/p/3125450.html
Copyright © 2011-2022 走看看