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();
}
}
}
谢谢阅读!