大家先看代码:class Student implements Comparable<Student>{
private String name;
private int age;
private float score;
public Student(String name,int age,float score){
this.name=name;
this.age=age;
this.score=score;
}
public String toString(){
return name+"\t\t"+age+"\t\t"+score;
}
public int compareTo(Student stu) {
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;
}
}
}
}
public class ComparableDemo01 {
public static void main(String[] args) {
Student stu[]={new Student("张三",20,90.0f),new Student("李四",22,90.0f),new Student("找刘",20,70.0f)};
java.util.Arrays.sort(stu);
for(int i=0;i<stu.length;i++){
System.out.println(stu[i]);
}
}}我的问题是:利用sort排序时,如何调用Compareto方法实现的
还有,在如下覆写的Compareto方法中,当前对象数组是怎么传递的,this.score 和stu.score是怎么取值的,返回-1,0,1分别代表小于 等于 大于 那么返回的这些数字是怎么比较的呢?谢谢各位啦!
public int compareTo(Student stu) {
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;
}
}
private String name;
private int age;
private float score;
public Student(String name,int age,float score){
this.name=name;
this.age=age;
this.score=score;
}
public String toString(){
return name+"\t\t"+age+"\t\t"+score;
}
public int compareTo(Student stu) {
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;
}
}
}
}
public class ComparableDemo01 {
public static void main(String[] args) {
Student stu[]={new Student("张三",20,90.0f),new Student("李四",22,90.0f),new Student("找刘",20,70.0f)};
java.util.Arrays.sort(stu);
for(int i=0;i<stu.length;i++){
System.out.println(stu[i]);
}
}}我的问题是:利用sort排序时,如何调用Compareto方法实现的
还有,在如下覆写的Compareto方法中,当前对象数组是怎么传递的,this.score 和stu.score是怎么取值的,返回-1,0,1分别代表小于 等于 大于 那么返回的这些数字是怎么比较的呢?谢谢各位啦!
public int compareTo(Student stu) {
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;
}
}
解决方案 »
- 迷茫 Java还是 linux。???
- 线程交替问题
- 如何用swing的布局实现简单的查询界面
- 如何取出包含在List内的LabelValueBean的lable值
- 我想实现在一个TextField里面输入诸如202.118.75.71这样的IP地址,点击一下一个“查找主机名”的按钮就在一个Label里面显示对应的主机名
- 数据保存出错,求救!
- byte[]到底可以有多长?
- 小问题一个,百分相送
- a samll jdbc program, please help to find the error, thanks a lot!!!
- 新手送分题~~~~
- MyEclipse连接上Oracle后查看Oracle里表的问题
- JavaSE 如何连接FTP
public static void sort(Object[] a) {
Object[] aux = (Object[])a.clone();
mergeSort(aux, a, 0, a.length, 0);
}
private static void mergeSort(Object[] src, Object[] dest, int low, int high, int off) {
int length = high - low; // Insertion sort on smallest arrays
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low &&
((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
swap(dest, j, j-1);
return;
} // Recursively sort halves of dest into src
int destLow = low;
int destHigh = high;
low += off;
high += off;
int mid = (low + high) >>> 1;
mergeSort(dest, src, low, mid, -off);
mergeSort(dest, src, mid, high, -off); // If list is already sorted, just copy from src to dest. This is an
// optimization that results in faster sorts for nearly ordered lists.
if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
System.arraycopy(src, low, dest, destLow, length);
return;
} // Merge sorted halves (now in src) into dest
for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}这下看明白了吧。知道是怎么调用的了吗?