前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Comparabe比较器排序

Comparabe比较器排序

作者头像
葆宁
发布2019-04-18 17:08:20
5200
发布2019-04-18 17:08:20
举报
文章被收录于专栏:FREE SOLOFREE SOLO

class Student implements Comparable { // 指定类型为Student private String name ; private int age ; private float score ; public Student(String name,int age,float score){ this.name = name ; this.age = age ; this.score = score ; } public String toString(){ return name + “\t\t” + this.age + “\t\t” + this.score ; } public int compareTo(Student stu){ // 覆写compareTo()方法,实现排序规则的应用 if(this.score>stu.score){ return -1 ; }else if(this.score<stu.score){ return 1 ; }else{ if(this.age>stu.age){ return 1 ; }else if(this.age<stu.age){ return -1 ; }else{ return 0 ; } } } }; public class ComparableDemo01{ public static void main(String args[]){ Student stu[] = {new Student(“张三”,20,90.0f), new Student(“李四”,22,90.0f),new Student(“王五”,20,99.0f), new Student(“赵六”,20,70.0f),new Student(“孙七”,22,100.0f)} ; java.util.Arrays.sort(stu) ; // 进行排序操作 for(int i=0;i<stu.length;i++){ // 循环输出数组中的内容 System.out.println(stu[i]) ; } } };

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年03月28日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档