字典---有序字典(SortedDictionary<TKey,TValue>)
--------------------------------------------------------------EmployeeID.cs(键)
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Collections;publicclassEmployeeID:IComparable<EmployeeID>{publicintID{get;privateset;}publicEmployeeID(intid){this.ID=id;}publicintCompareTo(EmployeeIDother){returnID.CompareTo(other.ID);}}
--------------------------------------------------------------Person.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceConsoleApplication1{publicclassPerson{publicstringCountry{get;privateset;}publicstringName{get;privateset;}publicintAge{get;privateset;}publicPerson(stringcountry,stringname,intage){this.Country=country;this.Name=name;this.Age=age;}}}
--------------------------------------------------------------主程序
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceConsoleApplication1{classProgram{staticvoidMain(string[]args){//SortedList<TKey,TValue>使用的内存少//SortedDictionary<TKey,TValue>元素插入和删除速度快//键要实现IComparable<inT>接口SortedDictionary<EmployeeID,Person>sd=newSortedDictionary<EmployeeID,Person>();sd.Add(newEmployeeID(3),newPerson("中国","张飞",40));sd.Add(newEmployeeID(20),newPerson("中国","关羽",43));sd.Add(newEmployeeID(4),newPerson("中国","刘备",45));sd.Add(newEmployeeID(5),newPerson("中国","诸葛亮",24));sd.Add(newEmployeeID(1),newPerson("美国","豪威尔",40));sd.Add(newEmployeeID(0),newPerson("美国","奥巴马",40));sd.Add(newEmployeeID(210),newPerson("朝鲜","金三胖",40));sd.Add(newEmployeeID(80),newPerson("印度","印度阿三",40));foreach(variteminsd){Console.WriteLine(item.Key.ID+","+item.Value.Name);//键,正序排序}Console.ReadKey();}}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。