当前位置: 首页 > news >正文

广州市门户网站建设品牌监理网站建设价格多少

广州市门户网站建设品牌,监理网站建设价格多少,兰州seo整站优化服务商,网站套网站代码目录 1.系统排序原理 2.方式一#xff1a;调用接口并重写 3.方式二#xff1a;传排序规则函数做参数 1.系统排序原理 当我们对一个Listint类型的数组如list1排序时#xff0c;一个轻松的list1.sort();帮我们解决了问题 但是在实际应用过程中#xff0c;往往我们…目录 1.系统排序原理 2.方式一调用接口并重写 3.方式二传排序规则函数做参数 1.系统排序原理 当我们对一个Listint类型的数组如list1排序时一个轻松的list1.sort();帮我们解决了问题 但是在实际应用过程中往往我们遇到的问题会更加棘手 比如这样一个类 class BagItem{public int id;public string name;public int count;public BagItem(int id, string name, int count){this.id id;this.name name;this.count count;}} 这是一个背包物品类包含了物品编号id物品名称name物品数量count ListBagItem bagItems new ListBagItem();//传入参数依次为id,name,countbagItems.Add(new BagItem(1, 生命药水, 5));bagItems.Add(new BagItem(2, 魔力药水, 12));bagItems.Add(new BagItem(3, 速度药水, 7));bagItems.Add(new BagItem(4, 铁皮药水, 3));bagItems.Add(new BagItem(5, 重力药水, 9));bagItems.Add(new BagItem(6, 回城药水, 2));bagItems.Sort(); 当我们创建一个ListBagItem数组并想对它排序时猜猜会发生什么 这时系统会报错 想要弄明白为什么报错需要先解释一个sort排序的原理 sort排序时会用到CompareTo函数这个函数方法存放在IComparable接口中 之前我们Listint类型数组排序能够成功是因为int类调用了上述接口并实现所以sort排序时可以成功运行 可以看到int类调用了IComparable接口 而我们自己写的类调用不到对应类型的函数所以运行失败 接下来将介绍两种方法时得我们自己写的类可以成功排序 2.方式一调用接口并重写 既然int类可以调用我们也可以给自己写的类调用这个接口 class BagItem:IComparableBagItem{public int id;public string name;public int count;public BagItem(int id, string name, int count){this.id id;this.name name;this.count count;}public int CompareTo(BagItem other){//返回类型int//返回值0时当前成员排在other成员右边//返回值0时当前成员排在other成员左边//可以理解为other成员处于0位置if (other.count this.count || (other.count this.count other.id this.id))return -1;elsereturn 1;}}internal class Program{static void Main(string[] args){ListBagItem bagItems new ListBagItem();//传入参数依次为id,name,countbagItems.Add(new BagItem(3, 生命药水, 5));bagItems.Add(new BagItem(2, 魔力药水, 12));bagItems.Add(new BagItem(1, 速度药水, 12));bagItems.Add(new BagItem(5, 铁皮药水, 3));bagItems.Add(new BagItem(4, 重力药水, 9));bagItems.Add(new BagItem(6, 回城药水, 2));foreach (BagItem item in bagItems){Console.WriteLine(物品{0}id{1}数量{2},item.name,item.id,item.count);}bagItems.Sort();Console.WriteLine(---------------------);foreach (BagItem item in bagItems){Console.WriteLine(物品{0}id{1}数量{2}, item.name, item.id, item.count);}}} 3.方式二传排序规则函数做参数 当我们查看sort用法时我们可以看到它还有其他重载类型 我们默认使用的是第三种无参数的类型如果我们写一个函数作为参数传入时那么就可以使用第一类型传入我们自己的比较规则。 internal class Program{static void Main(string[] args){ListBagItem bagItems new ListBagItem();//传入参数依次为id,name,countbagItems.Add(new BagItem(3, 生命药水, 5));bagItems.Add(new BagItem(2, 魔力药水, 12));bagItems.Add(new BagItem(1, 速度药水, 12));bagItems.Add(new BagItem(5, 铁皮药水, 3));bagItems.Add(new BagItem(4, 重力药水, 9));bagItems.Add(new BagItem(6, 回城药水, 2));foreach (BagItem item in bagItems){Console.WriteLine(物品{0}id{1}数量{2},item.name,item.id,item.count);}bagItems.Sort(SortBagItems);Console.WriteLine(---------------------);foreach (BagItem item in bagItems){Console.WriteLine(物品{0}id{1}数量{2}, item.name, item.id, item.count);}}static int SortBagItems(BagItem left,BagItem right){//返回类型int//返回值0时当前成员排在other成员右边//返回值0时当前成员排在other成员左边//可以理解为other成员处于0位置if (right.count left.count || (right.count left.count right.id left.id))return -1;elsereturn 1;}}
http://www.yingshimen.cn/news/140810/

相关文章:

  • 厦门网站建设费用wordpress设置301
  • iis7新建网站网站模板整站
  • 火的网站建设明细报价表北京低价网站建设
  • 合肥搭建网站wordpress好友添加
  • 虚拟主机搭建多个网站厦门市住房建设网站
  • 网站建设中英文域名购买成功后如何使用
  • wordpress的站点是什么公司部门组织架构
  • 如何撤销网站上信息成都seo
  • 宜宾住房与城乡建设部网站一流的网站建设流程图
  • 悬赏做logo的网站具有营销价值好的网站
  • 建设网站需要学习什么poedit2 汉化wordpress
  • 湖北工程建设招投标中心网站网页兼容性 网站开发
  • 摄影网站源码学做网站教程视频
  • 在网上那里能接单做网站网站正在建设中模板下载
  • 网站地图生成器哪个好久久建筑网会员登录没有签到得金币了吗
  • 按揭车在哪个网站可以做贷款wordpress 广告
  • 公司网站建设概述深圳网页设计网页制作
  • 重庆网站推广运营明月 WordPress
  • 知名品牌网站有哪些网站的命名规则
  • 南京网站建设流程正能量不良网站直接进入免费
  • 网站教育机构排行前十名广东网站建设公司报价表
  • 为什么要加强网站安全建设精品下载
  • 做筹款的网站需要什么资质保定网站建设公司哪家好
  • 做企业网站广州网页设计与制作教程试题
  • wordpress 换域名 全站301重定向wordpress下载及安装
  • 射阳网站建设注册号域名后如何建设公司网站
  • 刚开今天新开传奇网站网站里面添加支付怎么做
  • 门户网站开发 报价云服务器可以自己搭建吗
  • 研究院网站建设方案笨笨网站建设专家
  • 青岛网站设计如何做企业信息查询系统官网山东省