【问题标题】:Implementing compareTo() and Comparable-interface实现 compareTo() 和 Comparable-interface
【发布时间】:2017-11-06 10:41:53
【问题描述】:

我正在尝试实现ComparablecompareTo(),但似乎无法使其工作。我一直在尝试不同的方法,但我真的不明白。我知道我应该实现类似的接口,并且在使用它之前我需要创建方法compareTo()(对我来说很奇怪,从 python 到面向对象的编程)。

我希望它比较两个人的年龄,所以我尝试编写如下所示的代码,但似乎无法使用 compareTo()。我收到错误消息:“此方法必须返回 int 类型”,但正如我所见,我只返回 1、-1 和 0,int? p>

另外,我知道最后的打印行是错误的。我该如何更改它,例如:“Name1,25 岁​​,比 Name2,21 岁大”。在 python 中,我可以从列表中提取 2 个特定值并将它们与某些给定方法进行比较;我不确定如何在 Java 中提取 2 个不同的值。

import java.util.ArrayList;
import java.util.Random;

class Human implements Comparable<Human>{

    int age;
    String name;

    public Human (int myAge, String myName) {
        name = myName;
        age = myAge;

    }

    public Human() {
        this(randomAge(),randomName());
    }

    public int compareTo(Human o) {
        if (this.age > o.age) {
            return 1;
        }
        if (this.age < o.age) {
            return -1;
        }
        if (this.age == o.age) {
            return 0;
        }
    }

    protected static int randomAge() {
        Random randomGenerator = new Random();      
        return randomGenerator.nextInt(99);
    }

    protected static String randomName() {
        Random randomGenerator = new Random();
        return "Name"+randomGenerator.nextInt(15);
    }

    public int getAge(){
        return age;
    }

    public String getName() {
        return name;
    }

    public String toString() {
        return "\nName: " + name + "\nAge: " + age + " yrs old\n";
    }

    public static void main (String[] args) {

        ArrayList<Human> randomHumans = new ArrayList<Human>();
        for (int j=0;j<2;j++) {
            Human randomPerson = new Human();
            randomHumans.add(randomPerson);
        }
        System.out.println(insert.something.here);
    }

}

【问题讨论】:

  • 答案很简单,编译器不知道至少有一个 if 子句将被输入,因此认为可能存在没有返回语句的情况。将其更改为使用 if/else if/else 它应该可以工作。或者你也可以return this.age - o.age
  • 你应该使用Integer.compare()来避免溢出
  • @OHGODSPIDERS 我现在在我的代码中使用return this.age - o.age,它没有给我任何错误消息。但是当我写System.out.println(randomHumans.get(0).compareTo(randomHumans.get(1))); 时,我得到输出“38”。不明白为什么,它应该只返回 1、-1 或 0。我认为它只是返回年龄的差异。
  • 您可以简单地使用三元运算符,使代码缩短为一行!这是代码:>> this.age>o.age ? 1 :this.age
  • 你误会了。 compareTo 文档指出它“返回负整数、零或正整数,因为此对象小于、等于或大于指定对象。”任何大于 0 的返回数都被视为“此对象被认为大于指定对象”,因此返回 1 或返回 40562 都没有关系。任何负数也是如此。

标签: java comparable compareto


【解决方案1】:

所以使用三个 if 语句不起作用,这就是为什么我可以编写一个 if/else if/else 语句,但另一个更简单的解决方案是在compareTo(Human o)-方法中编写它;

return this.age - o.age

那么对于打印到底;

int b = randomHumans.get(0).compareTo(randomHumans.get(1));

if (b>0) {
    System.out.println(randomHumans.get(0).getName()+", "+randomHumans.get(0).getAge()+" yrs old, is older than "+randomHumans.get(1).getName()+", "+randomHumans.get(1).getAge()+" yrs old.");
}
else if (b<0) {
    System.out.println(randomHumans.get(0).getName()+", "+randomHumans.get(0).getAge()+" yrs old, is younger than "+randomHumans.get(1).getName()+", "+randomHumans.get(1).getAge()+" yrs old.");
}
else {
    System.out.println(randomHumans.get(0).getName()+", "+randomHumans.get(0).getAge()+" yrs old, is just as old as "+randomHumans.get(1).getName()+", "+randomHumans.get(1).getAge()+" yrs old.");
}

【讨论】: