java.util.Arrays类也可以对Object数组进行排序,但是要使用这种方法排序必须实现Comparable接口,此接口就是用于指定对象排序规则的。

设计一个学生类,成绩由高到低排序,成绩相等,按年龄由低到高排序。

//=================================================
// File Name : Array_demo
//------------------------------------------------------------------------------
// Author : Common // 类名:student_
// 属性:
// 方法:
class student_ implements Comparable<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 String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + 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;
}
}
} } //主类
//Function : Comparable_demo
public class Comparable_demo { public static void main(String[] args) {
// TODO 自动生成的方法存根
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]);
}
} }

Comparable接口的更多相关文章

  1. 12.Java中Comparable接口,Readable接口和Iterable接口

    1.Comparable接口 说明:可比较(可排序的) 例子:按照MyClass的y属性进行生序排序 class MyClass implements Comparable<MyClass> ...

  2. java中的Comparable接口

    类对象之间比较"大小"往往是很有用的操作,比如让对象数组排序时,就需要依赖比较操作.对于不同的类有不同的语义.如Student类,比较2个学生对象可以比较他们的score分数来评判 ...

  3. comparator接口与Comparable接口的区别

    1. Comparator 和 Comparable 相同的地方 他们都是java的一个接口, 并且是用来对自定义的class比较大小的, 什么是自定义class: 如 public class Pe ...

  4. Comparable接口与Comparator接口的区别

    1. Comparator 和 Comparable 相同的地方 他们都是java的一个接口, 并且是用来对自定义的class比较大小的, 什么是自定义class: 如 public class Pe ...

  5. Java集合中Comparator和Comparable接口的使用

    在Java集合中,如果要比较引用类型泛型的List,我们使用Comparator和Comparable两个接口. Comparable接口 -- 默认比较规则,可比较的 实现该接口表示:这个类的实例可 ...

  6. Java中的Comparable接口和Comparator接口

    Comparator位于包java.util下,比较器,是在集合外部定义排序.Comparable位于包java.lang下,代表当前对象可比较的,是在集合内部实现排序. Comparable代表一个 ...

  7. C#实现Comparable接口实现排序

    C#中,实现排序的方法有两种,即实现Comparable或Comparer接口,下面简单介绍实现Comparable接口实现排序功能. 实现Comparable接口需要实现CompareTo(obje ...

  8. Java之Comparable接口和Comparator接口

    Comparable & Comparator 都是用来实现集合中元素的比较.排序的: Comparable 是在集合内部定义的方法实现的排序: Comparator 是在集合外部实现的排序: ...

  9. 第12条:考虑实现Comparable接口

    CompareTo方法没有在Object中声明,它是Comparable接口中的唯一的方法,不但允许进行简单的等同性比较,而且允许执行顺序比较.类实现了Comparable接口,就表明它的实例具有内在 ...

随机推荐

  1. jQuery自定义插件

    jQuery自定义插件 jQuery自定义插件按照功能分类,可以分为三类, 1>封装对象方法的插件,(也就是基于某个DOM元素的jQuery对象,局部的) 2>封装全局函数的插件,   ( ...

  2. 获取客户端ip地址

    新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:http://int.dpool. ...

  3. SVN的简单使用

    鼠标右键 SVNCheckout 导出文件 如果想在文件里做修改:右击鼠标-SVN Commit 如果想添加文件:把想要添加的文件粘贴到文件夹中,鼠标右击-TortoiseSVN-add 如果想在添加 ...

  4. iOS开发小技巧--TableView中headerView的循环利用,以及自定义的headerView

    一.首先要搞清楚,tableView中有两种headerView,一个是tableHeaderView,另一个是headerView.前者就一个;后者根据session决定个数 headerView的 ...

  5. Spring_SpEL

    一.本文目录         简单介绍SpEL的概念和使用     二.概念 Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言.语法类似于 EL:SpEL ...

  6. perl sub

    #/usr/bin/perl -w use strict; my $usage = "\n\nusage: $0 <length>\n\n"; my $length = ...

  7. [bzoj3694]最短路

    Description 给出一个$n$个点$m$条边的无向图,$n$个点的编号从$1-n$,定义源点为$1$. 定义最短路树如下:从源点$1$经过边集$T$到任意一点$i$有且仅有一条路径,且这条路径 ...

  8. 【BZOJ-2836】魔法树 树链剖分

    2836: 魔法树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 323  Solved: 129[Submit][Status][Discuss] ...

  9. 【BZOJ-1076】奖励关 概率与期望 + 状态压缩DP

    1076: [SCOI2008]奖励关 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1602  Solved: 891[Submit][Status ...

  10. C#变量类型

    在C#语言中,我们把变量分为七种类型,它们分别是:静态变量(static varibles),非静态变量(instance variables),数组元素(array elements),值参数(va ...