Java compareTo 方法初学者水平

Java compareTo method beginner level

我有一个作业需要创建一个学生 class,您可以在其中存储学生的 neptun 代码(字符串 nep_c)和您在考试中获得的分数(int point_num). 准备一个public int getMark() 方法returns 获得的ticket作为分数的函数table:

class实现了Comparable接口和compareTo()方法根据neptun代码对学生进行排序。在其中,根据您获得的分数。 不幸的是我不明白 comperTo 方法。你能帮我写出正确的代码吗?

public class Student implements Comparable{

private String nep_c;
private int point_num;
private int Mark;

public Student(int point_num, String nep_c) {
    this.point_num = 65;
    this.nep_c= "AAA1BB1";
}

public int getMark(){
    if (point_num <= 100 && point_num > 90)
    Mark = 5;
    else if (point_num <= 90 && point_num > 80)
    Mark = 4;
    else if (point_num <= 80 && point_num > 70)
    Mark = 3;
    else if (point_num <= 70 && point_num > 60)
    Mark = 2;
    else if (point_num <= 60)
    Mark = 1;
    else{
        return 0;
    }
    return Mark;
}

public String getNep_c(){
    return nep_c;
}

public int getPoint_num(){
    return point_num;
}

@Override
public int compareTo (Object o){
    return ???;
}

}

sort the students based on the neptun code

两部分。第一部分,改变

implements Comparable

implements Comparable<Student>

然后

@Override
public int compareTo(Student o) {
    return this.nep_c.compareTo(o.nep_c);
}

然而,你然后说在其中,根据你得到的分数。所以也许你真的想要

@Override
public int compareTo(Student o) {
    return Integer.compare(getMark(), o.getMark());
}

如果你想按 neptun 代码排序,并使用标记作为决胜局,那么你可以这样做

int c = this.nep_c.compareTo(o.nep_c);
if (c != 0) {
    return c;
}
return Integer.compare(getMark(), o.getMark());

或者,在 Java 8+ 中,使用 Comparator.comparing like

return Comparator.comparing(Student::getNep_c)
        .thenComparingInt(Student::getMark).compare(this, o);

compareTo 得到 Object 因为你实现了 Comparable,而不是通用的 Comparable<Student>。这就是为什么很难看到需要做什么的原因。

按如下方式更改您的代码:

public class Student implements Comparable<Student> {
    ...
    @Override
    public int compareTo(Student other) {
        ...
    }
}

现在在实施中将此学生的 nep_cother.nep_c 进行比较。如果这两个不相等,比较的结果是return;否则 return 比较标记的结果。

注意:您的 getMark 方法存在问题:它 returns 1 适合 60 分的学生return 2,并且它还分配私有Mark字段,可以转换为局部变量。

compareTo method on a Comparable 取一个值与当前对象进行比较,并且应该 return:

  • -1如果当前对象在另一个对象之前(可以使用任何负整数),
  • 0如果两个对象相等,
  • 1 如果当前对象在另一个对象之后(可以使用任何正整数)。

如果你想通过两个不同的字段比较两个对象,你可以执行以下操作(确保实现 Comparable<Student>):

@Override
public int compareTo(Student other) {
    final int comparedNepCode = nep_c.compareTo(other.nep_c);
    if (comparedNepCode == 0) {
        return Integer.compare(getMark(), other.getMark());
    }
    return comparedNepCode;
}

比较数字时,当前数减去另一个数为升序,所以:

  • x < y <=> x - y < 0
  • x = y <=> x - y = 0
  • x > y <=> x - y > 0

See the Java compaterTo withibn Comparable for info

compareTo 是一个函数,它应该 return 一个整数。这个想法是(例如)任何外部排序算法都使用它来比较一个 object (在你的情况下是 Student 类型)与另一个。在排序例程中(您可能不会写:它是集合的 built-in),它需要一种计算方法,它与另一个 Student 相比是 'highest',这就是 compareTo 提供的。

因此,在您的情况下,在 compareTo 中,它类似于...

return -1 if Object o.point_num < this.point_num
return 1 if Object o.point_num > this.point_num
return 0 if Object o.point_num = this.point_num

(顺便说一下,这不是 java 代码,只是注释)

请注意,compareTo 实际上并不进行排序。为此,您需要 Student object 的一些 Collection(一种数组类型)。 Collection object 应该有一个 '.sort' 方法 built-in.

至于哪种collection:看你的需求了。 Google 是你的朋友:-)

compareTo 方法用于通过在方法主体中指定要比较的参数来比较两个对象。但我想你已经明白了那部分。

您必须 return 一个基于您尝试进行的比较的整数值。 return有3种情况:

  • 0 - 如果您比较的参数相等。
  • 任何正整数 - 如果参数大于你想要的对象 正在从以下位置调用方法。
  • 任何负数 - 如果参数小于你的对象 从中调用方法。

例如,我使用 -1 if this > argument,0 if this = argument,1 if this < argument。 然后,排序方法将负责根据方法的 return 值对项目进行排序。 Here's an example