比较器的使用
来源:互联网 发布:mac os 10.13原版镜像 编辑:程序博客网 时间:2024/06/06 20:42
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://haiyuanxi.blog.51cto.com/4230602/931163
class Student implements Comparable<Student> {// 指定类型为Student
private String name;
private int age;
private float score;
public Student(String name, int age, float score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
@Override
public int compareTo(Student student) {
// 覆写compareTo()方法,实现排序规则的应用
// 先按成绩排
if (this.score > student.score) {
// 成绩大
return -1;//改变1和-1的值就可以改变排序的顺序
} else if (this.score < student.score) {
// 成绩小
return 1;
} else {
// 成绩相等再按年龄排序
if (this.age > student.age) {
// 年龄大
return 1;
} else if (this.age < student.age) {
// 年龄小
return -1;
} else {
return 0;
}
}
}
@Override
public String toString() {
// TODO Auto-generated method stub
return name + "\t\t" + this.age + "\t\t" + this.score;
}
}
public class ComparableDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu[] = { new Student("yanjun1", 20, 38),
new Student("yanjun2", 23, 44), new Student("yanjun3", 24, 44),
new Student("yanjun4", 20, 45) };
java.util.Arrays.sort(stu);//直接通过java.util.Arrays.sort(stu);排序
//按成绩排序
System.out.println("按成绩排序");
for (int i = 0; i < stu.length; i++) {
System.out.println(stu[i]);
}
}
}
private String name;
private int age;
private float score;
public Student(String name, int age, float score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
@Override
public int compareTo(Student student) {
// 覆写compareTo()方法,实现排序规则的应用
// 先按成绩排
if (this.score > student.score) {
// 成绩大
return -1;//改变1和-1的值就可以改变排序的顺序
} else if (this.score < student.score) {
// 成绩小
return 1;
} else {
// 成绩相等再按年龄排序
if (this.age > student.age) {
// 年龄大
return 1;
} else if (this.age < student.age) {
// 年龄小
return -1;
} else {
return 0;
}
}
}
@Override
public String toString() {
// TODO Auto-generated method stub
return name + "\t\t" + this.age + "\t\t" + this.score;
}
}
public class ComparableDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu[] = { new Student("yanjun1", 20, 38),
new Student("yanjun2", 23, 44), new Student("yanjun3", 24, 44),
new Student("yanjun4", 20, 45) };
java.util.Arrays.sort(stu);//直接通过java.util.Arrays.sort(stu);排序
//按成绩排序
System.out.println("按成绩排序");
for (int i = 0; i < stu.length; i++) {
System.out.println(stu[i]);
}
}
}
- 比较器的使用
- java 比较器的使用
- java比较器的使用
- 比较器Comparator的使用
- 比较器Comparator的使用
- 不要使用基于减法的比较器
- 使用BeyondCompare作为Subversive的比较器
- 使用MSP430模拟比较器的郁闷
- Comparator比较器的使用,Map排序
- 028.Comparator 比较器的使用
- java中Comparator比较器的使用
- Java比较器接口的使用
- BeautifulSoup使用的html解析器比较
- 【算法优化】比较器的使用
- JAVA 比较器使用
- 一些比较使用的网址
- std::set 比较的使用
- JTextArea比较详细的使用
- C1-Arrays and String【Cracking the Coding Interview 习题解答】
- UI图片处理工具
- Java静态变量的初始化(static块的本质)
- Blocks Programming Topics
- 【DFS】COGS 967. 观光旅游
- 比较器的使用
- spring mvc 返回json 乱码
- Websphere多个应用session相互覆盖问题解决办法
- 怎样实现cmd命令窗口的快速复制粘贴操作
- test
- cocos2dx教程之多点触控的实现
- strchr函数的简单实现
- 管道截获win 32 exe 的printf消息
- 关于报表里SQL执行速度的问题