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

导航网站开发工具windows优化大师官方下载

导航网站开发工具,windows优化大师官方下载,软件研发租用网站怎么做分录,网站建设资讯String类型的compareTo方法: 在String引用中,有一个方法可以比较两个字符串的大小: 和C语言中是一样的,两个字符串一个字符一个去比较。 那么这个方法是怎么实现的呢? 其实就是一个接口:Comparable接口里…

String类型的compareTo方法:

在String引用中,有一个方法可以比较两个字符串的大小:

和C语言中是一样的,两个字符串一个字符一个去比较。

那么这个方法是怎么实现的呢?

其实就是一个接口:Comparable接口里面的compareTo方法。

也就是如果能扩展这个接口,然后重写这个方法我们也可以在不同的类中实现自己想实现的方法。

比较两个学生年龄大小:

   @Overridepublic int compareTo(Object o) {Student student = (Student) o;return this.age- student.age;}

 比较两个学生名字长短:
 

    //比较两个学生名字大小:@Overridepublic int compareTo(Object o) {Student student = (Student) o;return this.name.compareTo(student.name);}

在main方法中测试:

用名字比较:

    public static void main(String[] args) {Student student1 = new Student(13,"zhangsan");Student student2 = new Student(15,"lisi");if(student1.compareTo(student2)>0){System.out.println("student1>student2");}else{System.out.println("student1<=student2");}}


用年龄比较:

两个结果是不一样的。

自写排序方法:

        我们可以借助该方法写一个排序方法:
 

    public static void mySort(Comparable[] comparables) {for (int i = 0; i < comparables.length - 1; i++) {for (int j = 0; j < comparables.length - 1 - i; j++) {//if(comparables[j] > comparables[j+1]) {if (comparables[j].compareTo(comparables[j + 1]) > 0) {//交换Comparable tmp = comparables[j];comparables[j] = comparables[j + 1];comparables[j + 1] = tmp;}}}}

用多态写是比较合理的!

当然也可以不用多态:

@Overridepublic int compareTo(Object o) {Student student = (Student) o;return this.name.compareTo(student.name);}public static void mySort(Student[] students){for(int i = 0;i<students.length-1;i++) {for (int j = 0; j <students.length-i-1; j++) {if(students[j].compareTo(students[j+1])>0){Student tmp = students[j];students[j] = students[j+1];students[j+1] = tmp;}}}}

建议用多态!!

http://www.ds6.com.cn/news/52928.html

相关文章:

  • 国内做网站建设知名的公司快手作品免费推广软件
  • wordpress jquery插件广州seo报价
  • 网站与网站链接怎么做上海seo推广平台
  • 深圳做网站电话国际新闻消息
  • 建设网站一般用什么字体百度域名收录提交入口
  • 建设各网站需要多久seo哪家公司好
  • 新桥专业网站建设上海做网站优化
  • 端游网络游戏排行榜重庆高端网站seo
  • 软件开发兼职网站互联网app推广具体怎么做
  • php在网站制作中的运行机制域名查询138ip
  • wordpress 付费内容优化大师软件下载
  • 网站经常出现502搜索引擎优化的核心是
  • 网站建设研究意义十大嵌入式培训机构
  • 用网站建设费用大数据统计网站
  • 做淘客哪个网站好点天津百度网络推广
  • 用php做网站教程icp备案查询
  • java做网站开发的流程甲马营seo网站优化的
  • 高德是中国的还是外国的无锡整站百度快照优化
  • 手机行业网站seminar是什么意思
  • wordpress 安全性设置网站seo优化推广外包
  • 上海做网站seo网站发稿平台
  • 用h5做的网站seo综合查询网站源码
  • 网站建设安全协议推广方式都有哪些
  • 阿里云做的网站怎么样软文新闻发稿平台
  • 深圳做地铁的公司网站广州seo服务公司
  • 百度网站的域名是什么搜狗搜索网页版
  • 黄骅港务汕头网站建设优化
  • 厦门 网站建设 闽icp手机注册网站
  • 杭州营销型网站制作宁波最好的推广平台
  • 那个网站做百科好过免费发广告的网站