compareTo

package 第11章;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;


class Stu implements Comparable<Stu> {
private int id;


/**
* @return the id
*/
public int getId() {
return id;
}


public Stu(int id, int score, String name) {
this.id = id;
this.score = score;
this.name = name;
}


/**
* @param id
*            the id to set
*/
public void setId(int id) {
this.id = id;
}


/**
* @return the score
*/
public int getScore() {
return score;
}


/**
* @param score
*            the score to set
*/
public void setScore(int score) {
this.score = score;
}


/**
* @return the name
*/
public String getName() {
return name;
}


/**
* @param name
*            the name to set
*/
public void setName(String name) {
this.name = name;
}


private int score;
private String name;


@Override
public String toString() {
return "id=" + this.id +"  "+ "score=" + this.score + " "+"name=" + this.name;


}


@Override
public int compareTo(Stu o) {
// TODO Auto-generated method stub


return this.score - o.score;
}


}


public class DataDemo7 {
/*
* (non-Javadoc)

* @see java.lang.Object#toString()
*/


public static void main(String[] args) {
Stu[] stus = { new Stu(1, 50, "1"), new Stu(2, 20, "2"),
new Stu(3, 40, "3"), new Stu(4, 40, "4"), new Stu(5, 10, "5") };
Arrays.sort(stus);
   for(int i=0;i<stus.length;i++)
   {
Stu  s1=  stus[i];
System.out.println(s1.toString());
   }
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值