无法将有机体与可比较的接口java进行适当的比较

sg24os4d  于 2021-07-12  发布在  Java
关注(0)|答案(3)|浏览(236)

所以我有一个java的“虚拟世界”项目,它有四种动物(它们扩展了有机体类)。每一种动物(羚羊、狐狸、乌龟、狼)都有一个主动性、力量和年龄的领域(所有的int)。我想把它们放在一个数组中,然后用最高的主动权排序,如果两种动物的主动权相等,那么年长的动物的主动权更高。为了比较这些,我的抽象类“有机体”实现了comparable接口,我重写了compareto方法,所以看起来像这样:

@Override
public int compareTo(Organism anotherOrganism) {
    int compareValue = 0;

    if (this.getInitiative() > anotherOrganism.getInitiative()){
        compareValue = 1;
    } else if (this.getInitiative() < anotherOrganism.getInitiative()){
        compareValue = -1;
    } else if (this.getInitiative() == anotherOrganism.getInitiative()){
        if (this.getAge() > anotherOrganism.getAge()){
            compareValue = 1;
        } else {
            compareValue = -1;
        }
    }
    return compareValue;
}

我按以下顺序将这些动物输入数组(括号中的数字是它们的实际主动性):狐狸(7)->海龟(1)->羚羊(4)->狼(5),然后使用

Collections.sort(organismList);

希望这会导致

Fox, Wolf, Antelope, Turtle

我收到的不是这个

Wolf, Antelope, Turtle, Fox

这里出了什么问题?很明显,动物也实施了compareto方法(它们指的是来自生物体的super.compareto)
编辑:是的,下面的建议说代码相当好,我已经删除了动物的任何主动性(字段留空),我仍然得到了非常相同的输出。不知道为什么。。。。

h5qlskok

h5qlskok1#

我已经把代码扩展成了即食餐。它打印出预期的结果。

[Organism$1Turtle, Organism$1Antelope, Organism$1Wolf, Organism$1Fox]

代码:

import java.util.*;

abstract class Organism implements Comparable<Organism> {
    public static void main(String[] args) throws Throwable {
        class Fox extends Organism {
            @Override public int getInitiative() {
                return 7;
            }
        }
        class Turtle extends Organism {
            @Override public int getInitiative() {
                return 1;
            }
        }
        class Antelope extends Organism {
            @Override public int getInitiative() {
                return 4;
            }
        }
        class Wolf extends Organism {
            @Override public int getInitiative() {
                return 5;
            }
        }
        List<Organism> organismList = Arrays.asList(
            new Fox(), new Turtle(), new Antelope(), new Wolf()
        );

        Collections.sort(organismList);

        System.err.println(organismList);
    }
    @Override public String toString() {
        return getClass().getName();
    }
    public abstract int getInitiative();
    public int getAge() {
        return 42;
    }
        @Override
public int compareTo(Organism anotherOrganism) {
    int compareValue = 0;

    if (this.getInitiative() > anotherOrganism.getInitiative()){
        compareValue = 1;
    } else if (this.getInitiative() < anotherOrganism.getInitiative()){
        compareValue = -1;
    } else if (this.getInitiative() == anotherOrganism.getInitiative()){
        if (this.getAge() > anotherOrganism.getAge()){
            compareValue = 1;
        } else {
            compareValue = -1;
        }
    }
    return compareValue;
}
}
t3irkdon

t3irkdon2#

你可以这样做:

public class Demo {

    public static void main(String[] args) {
        final List<Organism> organisms = new ArrayList<>();

        // Each organism is created with initiative, strength and age
        organisms.add(new Antelope(4, 1, 1));
        organisms.add(new Fox(7, 2, 2));
        organisms.add(new Turtle(1, 3, 3));
        organisms.add(new Wolf(5, 4, 4));
        organisms.add(new Wolf(5, 4, 5));

        organisms.sort(Demo::sortByInitiative);

        for (Organism o : organisms) {
            System.out.println(o);
        }
    }

    private static int sortByInitiative(Organism o1, Organism o2) {
        int c = Integer.compare(o2.getInitiative(), o1.getInitiative());
        if (c != 0) {
            return c;
        }
        return Integer.compare(o2.getAge(), o1.getAge());
    }
}
z5btuh9x

z5btuh9x3#

假设initiative和age成员变量是公共的(您可以用getter替换),您可以在一行代码中实现所需的排序

List<Organism> organisms = Arrays.asList(/* add your objects */);
organisms.sort(Comparator.comparing((Organism o) -> o.initiative, 
Comparator.reverseOrder()).thenComparing(o-> o.age, Comparator.reverseOrder()));

请参见下面的完整示例

public class Quick {

    static class Organism{
        public String name;
        public int initiative;
        public int age;

        public Organism(String name, int initiative, int age) {
            this.name = name;
            this.initiative = initiative;
            this.age = age;
        }

        @Override
        public String toString() {
            return "Organism{" +
                    "name='" + name + '\'' +
                    ", initiative=" + initiative +
                    ", age=" + age +
                    '}';
        }
    }

    public static void main(String[] args) {
        List<Organism> orgs = Arrays.asList(
                new Organism("Fox", 1, 2),
                new Organism("Ant", 1, 3),
                new Organism("Mule", 2, 2)
        );

        orgs.sort(Comparator.comparing((Organism o) -> o.initiative,
        Comparator.reverseOrder()).thenComparing(o-> o.age, Comparator.reverseOrder()));
        orgs.stream().forEach(System.out::println);

    }

}

相关问题