C# 數(shù)組查找與排序?qū)崿F(xiàn)代碼
Person p1 = new Person( " http://www.my400800.cn " , 18 );
Person p2 = new Person( " http://www.my400800.cn " , 19 );
Person p3 = new Person( " http://www.my400800.cn " , 20 );
Person[] persons = ... { p1, p2, p3 } ;
// 查找p2所在數(shù)組中的位置
Array.IndexOf < Person > (persons, p2);
2. 查找值
Person p1 = new Person( " http://www.my400800.cn " , 18 );
Person p2 = new Person( " http://blog.my400800.cn " , 19 );
Person p3 = new Person( " http:// blog.my400800.cn/400電話 " , 20 );
Person[] persons = ... { p1, p2, p3 } ;
Person p4 = new Person(p2.Name, p2.Age);
// 查找數(shù)組中與p4相同的元素所在的位置
Array.IndexOf < Person > (persons, p4);
但是,這種方法必需使Person重載Object的 Equals 比較方法
public override bool Equals( object obj)
... {
Person person = obj as Person;
if (person == null ) return false ;
return ( this .name == person.name && this .age == person.age);
}
第二種按對(duì)象的值查找的方法
實(shí)現(xiàn)IComparabler接口
public int CompareTo( object obj)
... {
Person person = obj as Person;
if (person == null )
throw new Exception( " The method or operation is not implemented. " );
// 先從年齡開(kāi)始比較
int ageResult = this .age.CompareTo(person.age);
if (ageResult == 0 )
... {
// 如果年齡相等在坐姓名比較
return this .name.CompareTo(person.name);
}
else
... {
return ageResult;
}
}
實(shí)現(xiàn)了IComparable接口后就可以使用Array.BinarySearch()進(jìn)行查找了
// 得到 person 在 persons 中有相同值的下標(biāo)
// 如果多個(gè)相同的值,BinarySearch將取最后
// 一個(gè)有相同值的數(shù)組下標(biāo)
Array.BinarySearch < Person > (persons, person);
注:使用Array.BinarySeach必須操作一個(gè)排序好的數(shù)組
3. 排序
只要對(duì)象實(shí)現(xiàn)了IComparable接口,就可以使用Array中靜態(tài)的方法Sort進(jìn)行排序
// 必需使比較的對(duì)象實(shí)現(xiàn)IComparable接口
Array.Sort < Person > (persons);
- C#二維數(shù)組基本用法實(shí)例
- C#使用foreach語(yǔ)句遍歷二維數(shù)組的方法
- C#實(shí)現(xiàn)對(duì)二維數(shù)組排序的方法
- c#基礎(chǔ)之?dāng)?shù)組與接口使用示例(遍歷數(shù)組 二維數(shù)組)
- C#數(shù)組排序的兩種常用方法
- C#基礎(chǔ)之?dāng)?shù)組排序、對(duì)象大小比較實(shí)現(xiàn)代碼
- C#實(shí)現(xiàn)對(duì)數(shù)組進(jìn)行隨機(jī)排序類(lèi)實(shí)例
- C#使用linq對(duì)數(shù)組進(jìn)行篩選排序的方法
- C#數(shù)組反轉(zhuǎn)與排序?qū)嵗治?/a>
- C#實(shí)現(xiàn)的二維數(shù)組排序算法示例
相關(guān)文章
.aspx中的命名空間設(shè)置實(shí)現(xiàn)代碼
原來(lái)以為.aspx 與.cs文件是屬于同一個(gè)類(lèi),今天才意識(shí)到自己錯(cuò)了。2009-04-04asp.net c# 調(diào)用百度pai實(shí)現(xiàn)在線翻譯,英文轉(zhuǎn)中文
本文詳細(xì)介紹asp.net c# 調(diào)用百度pai 實(shí)現(xiàn)在線翻譯以及英文轉(zhuǎn)中文實(shí)現(xiàn)代碼,需要了解的朋友可以參考下2012-12-12如何在網(wǎng)站級(jí)別動(dòng)態(tài)更改主題
如何在網(wǎng)站級(jí)別動(dòng)態(tài)更改主題...2007-04-04asp.net利用后臺(tái)實(shí)現(xiàn)直接生成html分頁(yè)的方法
這篇文章主要介紹了asp.net利用后臺(tái)實(shí)現(xiàn)直接生成html分頁(yè)的方法,比較簡(jiǎn)潔實(shí)用,需要的朋友可以參考下2014-08-08淺談.net core 注入中的三種模式:Singleton、Scoped 和 Transient
這篇文章主要介紹了淺談.net core 注入中的三種模式:Singleton、Scoped 和 Transient,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04ASP.NET Core AutoWrapper 自定義響應(yīng)輸出實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET Core AutoWrapper 自定義響應(yīng)輸出實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08C#實(shí)現(xiàn)pdf導(dǎo)出 .Net導(dǎo)出pdf文件
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)pdf導(dǎo)出 .Net導(dǎo)出pdf文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09