国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

探討如何用委托處理排序

2020-01-24 03:14:21
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class 冒泡排序
    {
        //首先要了解冒泡排序,其實(shí)很簡單就是索引前面的跟后面的比較,如果比后面的大2個(gè)值的位置就進(jìn)行調(diào)換
        static void Main()
        {
            int[] str ={ 0, 14, 3, 6, 1, 30, 10, 9, 28 };
            for (int i = 0; i < str.Length; i++)
            {
                for (int j = i + 1; j < str.Length; j++)
                {
                    if (str[j] < str[i])
                    {
                        int index = str[i];
                        str[i] = str[j];
                        str[j] = index;
                    }
                }
            }
            for (int m = 0; m < str.Length; m++)
            {
                Console.WriteLine(str[m]);
            }
        }
    }
}


復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    public delegate bool DelegateTest(object obj1, object obj2);
    class Class1
    {
        static void Main()
        {
            Employee[] Employees =
              {
                  new Employee("huguo",1000000),
                  new Employee("lili",20000),
                  new Employee("lulu",30000),
                  new Employee("xixi",50000),
                  new Employee("jianjian",10000),
                  new Employee("yoyo",9000)
              };
            //委托DelegateTest代理的方法是Greate
            DelegateTest MyTest = new DelegateTest(Employee.Greate);
            Sorter MySort = new Sorter();
            //冒泡算法中第一個(gè)參數(shù)是對應(yīng)Employees數(shù)組信息,第二個(gè)參數(shù)是委托
            MySort.Sort(Employees, MyTest);
            for (int m = 0; m < Employees.Length; m++)
            {
                Console.WriteLine(Employees[m].ToString());
            }
        }
    }
    class Employee
    {
        public string Name;
        public int Salary;
        public Employee(string Name, int Salary)
        {
            this.Name = Name;
            this.Salary = Salary;
        }
        //用override重寫string方法
        public override string ToString()
        {
            return string.Format(Name + ",{0:C},", Salary);
        }
        //定義一個(gè)方法,如果obj2傳過來的 Salary大于obj1就返回true;
        public static bool Greate(object obj1, object obj2)
        {
            Employee Employee1 = (Employee)obj1;
            Employee Employee2 = (Employee)obj2;
            return (Employee2.Salary > Employee1.Salary) ? true : false;
        }
    }
    class Sorter
    {
        public void Sort(object[] ArrayObj, DelegateTest Test)
        {
            //下面就是冒泡算法啦
            for (int i = 0; i < ArrayObj.Length; i++)
            {
                for (int j = i + 1; j < ArrayObj.Length; j++)
                {
                    if (Test(ArrayObj[j], ArrayObj[i]))
                    {
                        object Temp = ArrayObj[i];
                        ArrayObj[i] = ArrayObj[j];
                        ArrayObj[j] = Temp;
                    }
                }
            }
        }
    }

}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 梁河县| 新郑市| 兰西县| 渝中区| 胶南市| 龙泉市| 乌兰察布市| 方正县| 固原市| 古田县| 蒲江县| 南皮县| 南乐县| 乡城县| 成安县| 宿松县| 阜康市| 大港区| 铁力市| 辽宁省| 成安县| 罗源县| 巴马| 阿坝县| 泰兴市| 博乐市| 确山县| 关岭| 时尚| 长乐市| 宁国市| 额尔古纳市| 大理市| 合作市| 潼关县| 资中县| 临汾市| 嘉义县| 石门县| 墨竹工卡县| 陇西县|