Arrays只适合一个数组/对象内的数值进行比较,

Comparable比较器(Compara)适合数组,对象,队列等排序,

Comparable是一个接口类,实现此接口必须复写:compareTo 方法,

compareTo方法会返回三个值:1,0,-1,可以利用这三个值进行排序

//比较器
class Student2 implements Comparable<Student2>{
private int strno;
private String name;
private int age;
private float score; public Student2(int strno, String name, int age, float score) {
this.strno = strno;
this.name = name;
this.age = age;
this.score = score;
} @Override
public String toString() {
// TODO 自动生成的方法存根
return "学号:"+this.strno+",姓名:"+this.name+",年龄:"+this.age+",成绩:"+this.score;
}
@Override
public int compareTo(Student2 stu) {
// TODO 自动生成的方法存根
if(this.score > stu.score)
{
return 1;
}else if(this.score < stu.score){
return -1;
}else{
return 0;
}
} } public class CompareDemo { public static void main(String args[])
{
Student2 stu[] = {new Student2(1,"张三",20,98.13f),
new Student2(2,"李四",33,88.18f),
new Student2(3,"王五",41,80.32f),
new Student2(4,"赵六",28,89.77f),
new Student2(5,"田七",25,90.53f)}; //排序
System.out.println("++++++++++++数组排序前+++++++++++++++");
print(stu);
System.out.println("++++++++++++数组排序后+++++++++++++++");
Arrays.sort(stu);
print(stu);
} public static void print(Student2 stu[])
{
for(int i=0;i<stu.length; i++)
{ System.out.println(stu[i]);
}
} }

  

最新文章

  1. IOS开发之—— 在AFN基础上进行的网络请求的封装
  2. CharSequence cannot be resolved. It is indirectly referenced from required .class files
  3. 敏捷软件开发vs传统软件开发
  4. Delphi中函数定义和声明的位置
  5. bzoj4514: [Sdoi2016]数字配对--费用流
  6. Java基础之理解Annotation(与@有关,即是注释)
  7. 25.usb固件深入
  8. 与电子钱包相关的APDU指令
  9. 如何在Android应用程序中使用传感器模拟器SensorSimulator
  10. [引]MSDN Visual Basic 和 C# 中都会用到的编程概念
  11. 【转】Android开发中adb启动失败adb连接异常的解决办法 offline
  12. SSH框架整合 日志处理Spring结合 log4j、slf4j
  13. js中的各种“位置”——“top、clientTop、scrollTop、offsetTop……”,你知道多少
  14. v-cloak的用法和注意事项
  15. SpringBoot系列——jar包与war包的部署
  16. java面试一、1.5JVM
  17. word公式大小
  18. SQLServer: 解决“错误15023:当前数据库中已存在用户或角色”
  19. tp5模型笔记---多对多
  20. 表格(table)

热门文章

  1. 在腾讯云服务器上实现java web项目部署
  2. window 注册表上下文菜单如何配置?
  3. vue禁止复制的方式
  4. Python 中星号作用:解包&amp;打散
  5. python下多线程的限制以及多进程中传递参数的方式
  6. 常用模块一(random模块、time模块、sys模块)
  7. Chrome 正在受到自动化软件控制 – 解决办法
  8. linux c编程:System V消息队列一
  9. Android Studio的快捷键
  10. vue框架(一)