a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

C#基礎之數組排序、對象大小比較實現代碼

2020-01-24 03:42:54
字體:
來源:轉載
供稿:網友
從個小例子開始:
復制代碼 代碼如下:

int[] intArray = new int[]{2,3,6,1,4,5};
Array.Sort(intArray);
Array.ForEach<int>(intArray,(i)=>Console.WriteLine(i));

這個例子定義了一個int數組,然后使用Array.Sort(arr)靜態方法對此數組進行排序,最后輸出排序后的數組。以上例子將毫無意外的依次輸出1,2,3,4,5,6.
為什么Array的Sort方法可以正確的對int數組進行排序呢,我們自定義類可以嗎?試試看,如下代碼:
復制代碼 代碼如下:

public class Student
{
public int Age { get; set; }
public string Name { get; set; }
public int Score { get; set; }
}
static void Main(string[] args)
{
Student[] students = new Student[]{
new Student(){Age = 10,Name="張三",Score=70},
new Student(){Age = 12,Name="李四",Score=97},
new Student(){Age = 11,Name="王五",Score=80},
new Student(){Age = 9,Name="趙六",Score=66},
new Student(){Age = 12,Name="司馬",Score=90},
};
Console.WriteLine("--------------默認排序輸出--------");
Array.Sort(students);
Array.ForEach<Student>(students,(s)=>Console.WriteLine(string.Format("{0}{1,2}歲了,他的分數是{2,3}",s.Name,s.Age,s.Score)));
Console.Read();
}

我們定義了Student類然后同樣對他的數組進行排序,程序正確的編譯通過,但是運行出錯,運行時拋出了異常:System.InvalidOperationException{"Failed to compare two elements in the array."},這個異常的InnerException是ArgumentException{"At least one object must implement IComparable."};運行時異常說明:我們要使用Array.Sort(arr)靜態方法,必須得保證數組中有一個元素實現IComparable接口。既然如此我們就讓Student類實現IComparable接口.
復制代碼 代碼如下:

public class Student :IComparable
{
public int Age { get; set; }
public string Name { get; set; }
public int Score { get; set; }
/// <summary>
/// 實現IComparable接口,用Age做比較
/// </summary>
/// <param name="obj">比較對象</param>
/// <returns>比較結果</returns>
public int CompareTo(object obj)
{
if (obj is Student)
{
return Age.CompareTo(((Student)obj).Age);
}
return 1;
}
}

在Student類中實現了IComparable接口,在CompareTo方法中比較Student的Age屬性,這一次再次編譯運行,程序正常的輸出了按照年齡排序的Student數組。
假如說我們要對Student的Score屬性進行排序該怎么辦呢? Student類實現的IComparable接口只能按照一種屬性排序呀。
這個是很容易實現的.net的類庫開發者早為我們準備了另一個接口IComparer<T>接口用來實現比較類型T的兩個實例。如下StudentScoreComparer類實現了對Student按照Score屬性比較的IComparer<Student>
復制代碼 代碼如下:

public class StudentScoreComparer : IComparer<Student>
{
public int Compare(Student x, Student y)
{
return x.Score.CompareTo(y.Score);
}
}

現在我們可以使用下面代碼對Student數組按照Score屬性進行排序:
復制代碼 代碼如下:

Console.WriteLine("----------按分數排序輸出------------");
Array.Sort(students, new StudentScoreComparer());
Array.ForEach<Student>(students, (s) => Console.WriteLine(string.Format("{0}{1,2}歲了,他的分數是{2,3}", s.Name, s.Age, s.Score)));

不過一個簡單的按照Score屬性排序,再定義一個類是不是有點大題小作呀,有沒有更好的辦法呢?當然有. .net為我們準備了比較對象大小的委托Comparison<T>我們可以使用拉姆達表達式或者匿名委托直接排序,如下代碼實現:
復制代碼 代碼如下:

Console.WriteLine("----------按分數排序輸出----------");
Array.Sort(students, (s1, s2) => s1.Score.CompareTo(s2.Score));
Array.ForEach<Student>(students, (s) => Console.WriteLine(string.Format("{0}{1,2}歲了,他的分數是{2,3}", s.Name, s.Age, s.Score)));

完整代碼示例如下:
復制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SortingInCSharp
{
class Program
{
public class Student : IComparable
{
public int Age { get; set; }
public string Name { get; set; }
public int Score { get; set; }
/// <summary>
/// 實現IComparable接口,用Age做比較
/// </summary>
/// <param name="obj">比較對象</param>
/// <returns>比較結果</returns>
public int CompareTo(object obj)
{
if (obj is Student)
{
return Age.CompareTo(((Student)obj).Age);
}
return 1;
}
}
static void Main(string[] args)
{
Student[] students = new Student[]{
new Student(){Age = 10,Name="張三",Score=70},
new Student(){Age = 12,Name="李四",Score=97},
new Student(){Age = 11,Name="王五",Score=80},
new Student(){Age = 9,Name="趙六",Score=66},
new Student(){Age = 12,Name="司馬",Score=90},
};
Console.WriteLine("--------------默認排序輸出--------");
Array.Sort(students);
Array.ForEach<Student>(students, (s) => Console.WriteLine(string.Format("{0}{1,2}歲了,他的分數是{2,3}", s.Name, s.Age, s.Score)));
Console.WriteLine("----------按分數排序輸出------------");
Array.Sort(students, new StudentScoreComparer());
Array.ForEach<Student>(students, (s) => Console.WriteLine(string.Format("{0}{1,2}歲了,他的分數是{2,3}", s.Name, s.Age, s.Score)));
Console.WriteLine("----------按分數排序輸出----------");
Array.Sort(students, (s1, s2) => s1.Score.CompareTo(s2.Score));
Array.ForEach<Student>(students, (s) => Console.WriteLine(string.Format("{0}{1,2}歲了,他的分數是{2,3}", s.Name, s.Age, s.Score)));
Console.Read();
}
public class StudentScoreComparer : IComparer<Student>
{
public int Compare(Student x, Student y)
{
return x.Score.CompareTo(y.Score);
}
}
}
}

總結:
在C#中有三個關于比較對象大小的接口,分別是IComparable、IComparable<T>和IComparer<T>。 IComparable和IComparable<T>是類本身實現的在實例之間比較大小的行為定義。IComparer<T>是定義在被比較類之外的專門比較兩個T類型對象大小的行為,另外還有一個用于比較的委托定義Comparison<T>可以讓我們用拉姆達表達式或者匿名委托或方法更方便的排序。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: av在线一区二区三区 | 国产真实乱全部视频 | 欧美日韩在线播放 | 免费视频一区 | 97国产在线| 凹凸日日摸日日碰夜夜 | 国产精品视频免费 | 99精品欧美一区二区三区综合在线 | 国产中文字幕在线观看 | a中文在线 | 91精品国产日韩91久久久久久 | 久久久久国产精品午夜一区 | 99视频在线免费观看 | 我看午夜视频 | 中文字幕日韩一区二区 | 精品亚洲网 | 欧美一级在线观看 | 欧美成人h版在线观看 | 黑人巨大精品欧美一区二区一视频 | 久久高清片| 99热免费在线 | 娇妻被朋友调教成玩物 | 男女免费视频 | 第一福利丝瓜av导航 | 销魂美女一区二区三区视频在线 | 在线观看一区 | 欧美成人免费网站 | 国产激情精品视频 | 欧美一级艳片视频免费观看 | 免费看黄色的网站 | 精品毛片| 希岛爱理在线 | 亚洲中国字幕 | 99久久久免费视频 | 日韩视频精品 | 欧美精品一区二区蜜臀亚洲 | 久久国产精品视频 | 欧美精品亚洲精品 | 亚洲国产精品久久久久 | 欧美全黄| 在线观看欧美日韩视频 |