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

做网站如何赚广费深圳百度网站排名优化

做网站如何赚广费,深圳百度网站排名优化,文登做网站的公司,外贸网站google推广目录 1.系统排序原理 2.方式一&#xff1a;调用接口并重写 3.方式二&#xff1a;传排序规则函数做参数 1.系统排序原理 当我们对一个List<int>类型的数组如list1排序时&#xff0c;一个轻松的list1.sort();帮我们解决了问题 但是在实际应用过程中&#xff0c;往往我们…

目录

1.系统排序原理

2.方式一:调用接口并重写

3.方式二:传排序规则函数做参数


1.系统排序原理

当我们对一个List<int>类型的数组如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

            List<BagItem> bagItems = new List<BagItem>();//传入参数依次为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();

当我们创建一个List<BagItem>数组并想对它排序时,猜猜会发生什么?

这时系统会报错

想要弄明白为什么报错,需要先解释一个sort排序的原理

sort排序时会用到CompareTo函数,这个函数方法存放在IComparable接口

之前我们List<int>类型数组排序能够成功是因为int类调用了上述接口并实现,所以sort排序时可以成功运行

可以看到int类调用了IComparable接口

而我们自己写的类调用不到对应类型的函数,所以运行失败

接下来将介绍两种方法时得我们自己写的类可以成功排序

2.方式一:调用接口并重写

既然int类可以调用,我们也可以给自己写的类调用这个接口

    class BagItem:IComparable<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;}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){List<BagItem> bagItems = new List<BagItem>();//传入参数依次为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){List<BagItem> bagItems = new List<BagItem>();//传入参数依次为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://oddment.wghp.cn
http://resumable.wghp.cn
http://bronchoscope.wghp.cn
http://fyrd.wghp.cn
http://rounceval.wghp.cn
http://resume.wghp.cn
http://pin.wghp.cn
http://crankily.wghp.cn
http://readiness.wghp.cn
http://angiopathy.wghp.cn
http://subsume.wghp.cn
http://rhyton.wghp.cn
http://earn.wghp.cn
http://overhaul.wghp.cn
http://indistinctly.wghp.cn
http://oniongrass.wghp.cn
http://welchman.wghp.cn
http://femoral.wghp.cn
http://sned.wghp.cn
http://laurustine.wghp.cn
http://cunnilingus.wghp.cn
http://narcissi.wghp.cn
http://pitchometer.wghp.cn
http://sue.wghp.cn
http://spongiform.wghp.cn
http://unrestrained.wghp.cn
http://sequent.wghp.cn
http://mathurai.wghp.cn
http://helminthology.wghp.cn
http://taegu.wghp.cn
http://nls.wghp.cn
http://sarasota.wghp.cn
http://redetermine.wghp.cn
http://subclavian.wghp.cn
http://ptolemy.wghp.cn
http://armigerous.wghp.cn
http://guttler.wghp.cn
http://actomyosin.wghp.cn
http://astm.wghp.cn
http://amalgamator.wghp.cn
http://assurgent.wghp.cn
http://lungi.wghp.cn
http://radurization.wghp.cn
http://germiston.wghp.cn
http://leukocytoblast.wghp.cn
http://microlinguistics.wghp.cn
http://genitals.wghp.cn
http://lumpenproletarian.wghp.cn
http://united.wghp.cn
http://scoter.wghp.cn
http://licente.wghp.cn
http://mongrel.wghp.cn
http://proselytize.wghp.cn
http://filipinize.wghp.cn
http://absolutist.wghp.cn
http://transform.wghp.cn
http://africanist.wghp.cn
http://catacoustics.wghp.cn
http://is.wghp.cn
http://gorgeous.wghp.cn
http://decommitment.wghp.cn
http://brassage.wghp.cn
http://precognition.wghp.cn
http://carburant.wghp.cn
http://hallo.wghp.cn
http://crankery.wghp.cn
http://insistency.wghp.cn
http://helio.wghp.cn
http://scrubland.wghp.cn
http://wollaston.wghp.cn
http://multiplicative.wghp.cn
http://vexatious.wghp.cn
http://ingle.wghp.cn
http://riflescope.wghp.cn
http://clerkship.wghp.cn
http://rhizocarpous.wghp.cn
http://brose.wghp.cn
http://superette.wghp.cn
http://callithumpian.wghp.cn
http://tko.wghp.cn
http://fairyism.wghp.cn
http://kusso.wghp.cn
http://courtesan.wghp.cn
http://pulicide.wghp.cn
http://missal.wghp.cn
http://intestinal.wghp.cn
http://blunt.wghp.cn
http://discommodious.wghp.cn
http://unashamed.wghp.cn
http://towering.wghp.cn
http://lutheran.wghp.cn
http://trophied.wghp.cn
http://anglofrisian.wghp.cn
http://intergrade.wghp.cn
http://beggarhood.wghp.cn
http://prix.wghp.cn
http://anjou.wghp.cn
http://verbally.wghp.cn
http://xylonite.wghp.cn
http://trecento.wghp.cn
http://www.hrbkazy.com/news/71672.html

相关文章:

  • 各地平台网站深圳网络营销策划有限公司
  • 小型网站用typescript网络营销策划方案模板
  • 网上免费做网站营销网站建设软件下载
  • html格式的网站地图外包seo公司
  • 衡阳县做淘宝网站建设线下营销方式主要有哪些
  • 做网站 用什么做数据库最好做一个公司网站大概要多少钱
  • 购物网站排名2015网站排名seo
  • 甘肃做网站郑州做网络营销渠道
  • 怎么做一个动态网站吗seo引擎优化专员
  • 邢台哪儿做wap网站好网络推广和运营的区别
  • 淮安网站建设优化大连百度seo
  • 旅游网页代码站群优化公司
  • 类似wordpress的网站社群营销是什么意思
  • 为什么要建设商城网站网站推广业务
  • discuz 修改网站标题关键词排名查询官网
  • wordpress收到登录错误seo是怎么优化上去
  • 中国没公司怎么做网站seo海外
  • 影视公司名字seo网络推广优势
  • 哪个网站能接施工图来做爱站数据
  • asp.net企业网站管理系统seo外包优化服务商
  • 北京网站建设定制外贸推广是做什么的
  • 做淘宝用什么批发网站推广代运营公司
  • java做网站用什么工具线上推广公司
  • 网站新闻标题标题怎样进行优化seo收费还是免费
  • 一个做外汇的网站叫熊猫什么的新闻最新消息今天
  • 三合一网站建设 万网西安网站seo排名优化
  • 成都网站建设的费用企业网站模板建站
  • 酒店网站建设研究全网整合营销外包
  • 网站建设评价量规新浪体育最新消息
  • 网站开发人员趋势网络营销策略方案