如何对不同类类型的对象数组进行排序?

时间:2016-03-20 22:59:17

标签: java sorting hierarchy compareto

我正在尝试开发一个程序,它可以对不同类类型的对象数组进行排序,但是在相同的层次结构中。所有对象都列在我尝试排序的同一个数组中,虽然我可以按字母顺序对一个类型相同的对象数组进行排序,但我无法弄清楚如何使用相同的方法对所有对象进行排序Arrays.sort()方法。任何人都可以提供的帮助将不胜感激。

import java.util.Arrays;

public class Driver {

public static void main(String[] args) {

    Vehicle[] machines = new Vehicle[3];//Example of an array that I can sort
    machines[0] = new Vehicle("Giant Robot");
    machines[1] = new Vehicle("Time Machine");
    machines[2] = new Vehicle("Airplane");
    Arrays.sort(machines);
    for (int i = 0; i < machines.length; i++)
        System.out.println(machines[i].getName());


    Vehicle[] vehicles = new Vehicle[7];//example of an array that I cannot sort
    vehicles[0] = new Car("Batmobile", 10);
    vehicles[1] = new Helicopter("Batcopter", "x");
    vehicles[2] = new Car("Jaguar", 6);
    vehicles[3] = new Helicopter("RC Copter", "t");
    vehicles[4] = new Car("Accelerator", 6);
    vehicles[5] = new Helicopter("Stormshadow", "z");
    vehicles[6] = new Car("Batmobile", 11);
}

}

**

public class Vehicle implements Comparable {
private String name;

public Vehicle(){
    name = "no name";
}

public Vehicle(String newName){
    name = newName;
}

public String getName(){
    return name;
}

public int compareTo(Object o)
{
    if ((o != null) &&
        (o instanceof Vehicle))
    {
        Vehicle otherVehicle = (Vehicle) o;
        return (name.compareTo(otherVehicle.name));
    }
    return -1;
}

}

**

public class Car extends Vehicle {

private int tireSize;

public Car(){
    super();
    tireSize = 0;
}

public Car(String newName, int newTireSize){
    super(newName);
    tireSize = newTireSize;
}

public int getSize(){
    return tireSize;
}

}

**

public class Helicopter extends Vehicle {

private String bladeType;

public Helicopter(){
    super();
    bladeType = "none";
}

public Helicopter(String newName, String newBlade){
    super(newName);
    bladeType = newBlade;
}

public String getType(){
    return bladeType;
}

}

4 个答案:

答案 0 :(得分:1)

目标:您需要能够将Vehicle与其他Vehicle进行比较。

实现这一目标:

public class Vehicle implements Comparable<? extends Vehicle> {

    ....

    public int compareTo(Object o) {
        // Now, that the Comparable is for the type Vehicle
        // you know that o is some kind of vehicle

        // check vehicle related things
        // number of seats, dogs, whatever
        return -1;
    }

}

答案 1 :(得分:1)

您只需将代码调整为:

class Vehicle implements Comparable<Vehicle> {

    private String name;

    /* ... */

    @Override
    public int compareTo(Vehicle vehicle) {
        return name.compareTo(vehicle.getName());
    }
}

答案 2 :(得分:0)

在大多数情况下,您的类不应该实现Comparable,除非只有一个且只有一个顺序始终是正确的,如数字。您的车辆可以按名称,年龄和更多标准进行分类,因此不应实施Comparable

相反,您可以在实际对车辆进行排序时将排序功能作为lambda函数传递:

Arrays.sort(machines, (left, right) -> left.getName().compareTo(right.getName()));

或等同地:

Arrays.sort(machines, Comparator.comparing(Vehicle::getName));

这样您就不再需要implements Comparable

答案 3 :(得分:-1)

如果您想按车辆类型排序,那么在排序元素时需要考虑类类型。修改compareTo()方法,如下所示:

public int compareTo(Object o){
    if ((o != null) &&
        (o instanceof Vehicle)){
        Vehicle otherVehicle = (Vehicle) o;
        return (otherVehicle.getClass().getSimpleName().equals(this.getClass().getSimpleName()) ? 
                name.compareTo(otherVehicle.name) 
                : otherVehicle.getClass().getSimpleName().compareTo(this.getClass().getSimpleName()));
    }
    return -1;
}