======================================================
注:本文源代码点此下载
======================================================
今天上课,老师讲到arrays.sor()的时候说,这个可以对数组进行排序,于是当时脑海中立刻浮现出两个问题:一、如果对类排序,一定要把实现什么接口。二、实现了这个接口,java怎么知道一个类是否实现了某个接口。于是带着这个问题做了一翻查找。
对于类数组排序,调用arrays.sort()即可,但是也只是对于基本类型的支持,如果对类进行排序,有如下两种方法:
方法一,该类一定要实现comparable接口,并且实现public int compareto(t o);方法。比较结果大的返回1,相等返回0,小于返回-1。该接口实现了泛型,如果声明,则compareto的参数则为object。
实体类student:
1 public class student implements comparablestudent>{
2private string name = null;
3private int age = 0;
4public string getname() {
5return name;
6}
7public void setname(string name) {
8this.name = name;
9}
10public int getage() {
11return age;
12}
13public void setage(int age) {
14this.age = age;
15}
16public student(string name, int age) {
17this.name = name;
18this.age = age;
19}
20@override
21public string tostring() {
22return string.format("name=%s age=%d", this.name, this.age);
23}
24@override
25public int compareto(student o) {
26// 按名字排序
27return this.name.compareto(o.getname());
28}
29 }
声明一个student数组,并且调用arrays.sort()进行排序,然后输出
1 student[] stus = new student[3];
2 stus[0] = new student("flowers", 12);
3 stus[1] = new student("boys", 13);
4 stus[2] = new student("zero", 21);
5 arrays.sort(stus);
6for(student s : stus){
7system.out.println(s.tostring());
8 }
结果:
name=boys age=13
name=flowers age=12
name=zero age=21
方法二,如果student类并未实现comparable接口,则在调用arrays.sort()时,要指定一个“比较器”,一个接口类comparator,所以使用时同时要写出实现intcompare(t o1, t o2);方法的代码。调用代码如下:
1 arrays.sort(stus, new comparatorstudent>(){
2@override
3public int compare(student o1, student o2) {
4return o1.getname().compareto(o2.getname());
5}
6 });
对于集合的排列,如arraylist等实现了collection接口,list是继承于collection,所以实现list的同样适用。集合类的排序主要是用collections.sort方法,collections和collection是不一样的,前者是类,后者是接口。
一般我们主要使用两个方法:
1.collection.sort(list arg0);
这种是最简单的一种排序方法,只需要实现他的comparable 接口及实现public int compareto(object arg0)方法即可。
1 arrayliststudent> list = newarrayliststudent>(3);
2 list.add(new student("flowers", 36));
3 list.add(new student("dog", 23));
4 list.add(new student("about", 67));
5 collections.sort(list);
2.collection.sort(list arg0,comparator arg1)
这种加入了比较器,具有更大的灵活性,便于管理,比较器可作为内部静态类的,以便于管理。比较器必须实现comparator接口。
1 collections.sort(list, new comparatorstudent>(){
2@override
3public int compare(student o1, student o2) {
4// 按年龄排序
5return o1.getage() > o2.getage()? 1:(o1.getage() ==o2.getage()? 0: -1);
6}
7 });
以上两种方法,得到的结果都一样:
name=dog age=23
name=flowers age=36
name=about age=67
查看collection.sort的源代码,不难看出java的思路,先讲集合类转化为数组,然后调用arrays.sort方法进行排序,同时传递过去比较器,最后利用集合的迭代器将结果赋值回集合类中。
1 public static t> void sort(listt> list, comparator super t> c) {
2object[] a = list.toarray();
3arrays.sort(a, (comparator)c);
4listiterator i = list.listiterator();
5for (int j=0; ja.length; j++) {
6i.next();
7i.set(a[j]);
8}
9 }
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/