C# 數(shù)組查找與排序?qū)崿F(xiàn)代碼
更新時間:2009年10月10日 14:07:10 作者:
數(shù)組查找對象的方法一種是查找對象,一種是查找值
1. 查找對象
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);
}
第二種按對象的值查找的方法
實(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. " );
// 先從年齡開始比較
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)
// 如果多個相同的值,BinarySearch將取最后
// 一個有相同值的數(shù)組下標(biāo)
Array.BinarySearch < Person > (persons, person);
注:使用Array.BinarySeach必須操作一個排序好的數(shù)組
3. 排序
只要對象實(shí)現(xiàn)了IComparable接口,就可以使用Array中靜態(tài)的方法Sort進(jìn)行排序
// 必需使比較的對象實(shí)現(xiàn)IComparable接口
Array.Sort < Person > (persons);
復(fù)制代碼 代碼如下:
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. 查找值
復(fù)制代碼 代碼如下:
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 比較方法
復(fù)制代碼 代碼如下:
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);
}
第二種按對象的值查找的方法
實(shí)現(xiàn)IComparabler接口
復(fù)制代碼 代碼如下:
public int CompareTo( object obj)
... {
Person person = obj as Person;
if (person == null )
throw new Exception( " The method or operation is not implemented. " );
// 先從年齡開始比較
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)行查找了
復(fù)制代碼 代碼如下:
// 得到 person 在 persons 中有相同值的下標(biāo)
// 如果多個相同的值,BinarySearch將取最后
// 一個有相同值的數(shù)組下標(biāo)
Array.BinarySearch < Person > (persons, person);
注:使用Array.BinarySeach必須操作一個排序好的數(shù)組
3. 排序
只要對象實(shí)現(xiàn)了IComparable接口,就可以使用Array中靜態(tài)的方法Sort進(jìn)行排序
復(fù)制代碼 代碼如下:
// 必需使比較的對象實(shí)現(xiàn)IComparable接口
Array.Sort < Person > (persons);
您可能感興趣的文章:
- C#二維數(shù)組基本用法實(shí)例
- C#使用foreach語句遍歷二維數(shù)組的方法
- C#實(shí)現(xiàn)對二維數(shù)組排序的方法
- c#基礎(chǔ)之?dāng)?shù)組與接口使用示例(遍歷數(shù)組 二維數(shù)組)
- C#數(shù)組排序的兩種常用方法
- C#基礎(chǔ)之?dāng)?shù)組排序、對象大小比較實(shí)現(xiàn)代碼
- C#實(shí)現(xiàn)對數(shù)組進(jìn)行隨機(jī)排序類實(shí)例
- C#使用linq對數(shù)組進(jìn)行篩選排序的方法
- C#數(shù)組反轉(zhuǎn)與排序?qū)嵗治?/a>
- C#實(shí)現(xiàn)的二維數(shù)組排序算法示例
相關(guān)文章
Visual Studio 2017 針對移動開發(fā)的新特性匯總
Visual Studio是世界上最好的IDE之一,下面就讓我們一起來看看Visual Studio 2017中有哪些功能使得移動開發(fā)變得更加容易,感興趣的朋友通過本文學(xué)習(xí)下吧2017-05-05
Entity?Framework管理一對一實(shí)體關(guān)系
本文詳細(xì)講解了Entity?Framework管理一對一實(shí)體關(guān)系的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03
詳解ASP.NET Core 網(wǎng)站發(fā)布到Linux服務(wù)器
本篇文章主要介紹了ASP.NET Core 網(wǎng)站發(fā)布到Linux服務(wù)器 。具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04
總結(jié)Visual Studio下ASP.NET模板化控件中的數(shù)據(jù)綁定
.NET框架中提供了很多數(shù)據(jù)綁定的組件,這里我們就來總結(jié)Visual Studio下ASP.NET模板化控件中的數(shù)據(jù)綁定,需要的朋友可以參考下2016-06-06
asp.net 數(shù)據(jù)庫備份還原(sqlserver+access)
Asp.net 備份、還原Ms SQLServer及壓縮Access數(shù)據(jù)庫2008-11-11
IIS7偽靜態(tài)web.config配置的方法和規(guī)則
本文主要介紹IIS7上配置偽靜態(tài)的超簡單的新方法,安裝URLRewrite插件,配置web.config即可。2016-04-04
.net mvc頁面UI之Jquery博客日歷控件實(shí)現(xiàn)代碼
最近在做一個博客系統(tǒng),其他需要用到博客日歷控件,網(wǎng)上搜索了很多資料,其中大部分都是javascript的,經(jīng)過總結(jié)使用jquery實(shí)現(xiàn)了博客日歷效果。代碼如下2013-09-09

