Collection -> 用SortedList实现排序

王朝c#·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

using System;

using System.Collections;

namespace 集合的比较和排序

{

public class Efficience:IComparable

{

private int workHour;

private int outPut;

int IComparable.CompareTo(Object obj)

{

if(obj==null)

throw new ArgumentException("比较对象不能为空");

if(!obj.GetType().Equals(this.GetType()))

throw new ArgumentException("比较的两者类型不同");

Efficience objEffic=(Efficience)obj;

if(this.Effic>objEffic.Effic)

return 1;

if(this.Effic<objEffic.Effic)

return -1;

return 0;

}

public int WorkHour

{

set

{

if(value<0)

throw new ArgumentException("工作时间不能为{0}或负数");

workHour=value;

}

}

public int OutPut

{

set

{

if(value<0)

throw new ArgumentException("工作产出不能为负数");

outPut=value;

}

}

public float Effic

{

get

{

return (float)outPut/(float)workHour;

}

}

}

class Class1

{

[STAThread]

static void Main(string[] args)

{

Random rand=new Random();

Efficience[] effics=new Efficience[5];

string[] persons={"Xiaohua","Diana","YanYan","ZhuLin","LiXin"};

Console.WriteLine("生成的 Effics 数组");

//Console.WriteLine("effics.GetLowerBound(0)={0},effics.GetUpperBound(0)={1}",effics.GetLowerBound(0),effics.GetUpperBound(0));

for(int i=effics.GetLowerBound(0);i<=effics.GetUpperBound(0);i++)

{

effics[i]=new Efficience();

effics[i].WorkHour=rand.Next()%24;

effics[i].OutPut=rand.Next()%1000;

Console.WriteLine("Person={0},Effic={1}",persons[i],effics[i].Effic);

}

SortedList sortedList=new SortedList();

for(int i=effics.GetLowerBound(0);i<=effics.GetUpperBound(0);i++)

{

sortedList.Add(effics[i],persons[i]);

}

Console.WriteLine("从 sortedList 中读取内容");

foreach(Efficience effic in sortedList.GetKeyList())

{

Console.WriteLine("Person={0},Effic={1}",sortedList[effic],effic.Effic);

}

Console.Read();

}

}

}

谢谢阅读!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航