0

我试图按升序和降序对我的飞机进行排序。我有一个飞机的哈希图,我想比较它们,以便我可以通过按 timeLimitBeforeLand 对地图进行排序来获得下一架飞机和最后一架飞机。我写了一个 compareTo 方法,它看起来像:

//---------------------------------------------------------------------------------------
//  CompareTo() used with the Comparable implementation.
//---------------------------------------------------------------------------------------
        public int compareTo(Object arg0) 
        {
            if((arg0 != null) && (arg0 instanceof Plane))
            {
            Plane p = (Plane) arg0;
            return (int)Math.ceil(this.timeLimitBeforeLand - p.getLimitBeforeLand());
            }
            return 0;
        }

CompareTo 需要 timeLimitBeforeLand:

// ---------------------------------------------------------------------------------------
//      Name:        getTimeLimitBeforeLand.
//      Description: Get the time before every plane is going to land.
//---------------------------------------------------------------------------------------
        public double getTimeLimitBeforeLand()
        {
        double fuelConsumption;
        double timeLimitBeforeLand = 0;

        for (TreeMap<String, Plane> theEntry : airlineMap.values()) {
        for (Plane aPlane : theEntry.values()) {
        if (aPlane.getPlaneType() == aPlane.getPlaneType().AIRBUS) {
        System.out.println(" ");
        System.out.println(aPlane);
        fuelConsumption = 2;
        timeLimitBeforeLand = (double) (aPlane.getFuelRemaining() / fuelConsumption);
        System.out.println(timeLimitBeforeLand + " minutes to land.");
        System.out.println(" ");
        } else if (aPlane.getPlaneType() == aPlane.getPlaneType().CORPORATE) {
        System.out.println(" ");
        System.out.println(aPlane);
        fuelConsumption = 3;
        timeLimitBeforeLand = (aPlane.getFuelRemaining() / fuelConsumption);
        System.out.println(timeLimitBeforeLand + " minutes to land.");
        System.out.println(" ");
        } else if (aPlane.getPlaneType() == aPlane.getPlaneType().PRIVATE) {
        System.out.println(" ");
        System.out.println(aPlane);
        fuelConsumption = 4;
        timeLimitBeforeLand = (double) (aPlane.getFuelRemaining() / fuelConsumption);
        System.out.println(timeLimitBeforeLand + " minutes to land.");
        System.out.println(" ");
        }
        }
        }
        return timeLimitBeforeLand;
        }

到目前为止,我在 mainApp 中的尝试:

TreeMap<String, PlaneStore> map = new TreeMap<String, PlaneStore>();

        ArrayList<Plane> copyList = new ArrayList<Plane>(map.);

        Plane comp = new Plane();

        Collections.sort(copyList, plane);

飞机类:

//---------------------------------------------------------------------------------------
//  Name:           Imports. 
//  Description:    To allow the use of different Java classes.
//---------------------------------------------------------------------------------------
import java.io.Serializable;
//---------------------------------------------------------------------------------------
//Name:         Class declaration. 
//---------------------------------------------------------------------------------------
public class Plane implements Comparable, Serializable
{   
//---------------------------------------------------------------------------------------
//  Variable declarations.
//---------------------------------------------------------------------------------------
    private String flightNumber;
    public String airlineName;
    private double fuelRemaining;
    private int overdue;
    private int passengerNumber;
//---------------------------------------------------------------------------------------
//  Enum declaration.
//---------------------------------------------------------------------------------------
    private AIRPLANETYPE planeType;
    private boolean isLanded = false;
    public double timeLimitBeforeLand;
//---------------------------------------------------------------------------------------
//  Enum Constuctor.
//---------------------------------------------------------------------------------------
    public enum AIRPLANETYPE
    {
        AIRBUS("1"), CORPORATE("2"), PRIVATE("3");

        private String planeName;

        private AIRPLANETYPE(String planeName)
        {
            this.planeName = planeName;
        }

        public String getPlaneName()
        {
            return this.planeName;
        }
    }
//---------------------------------------------------------------------------------------
//  Constructor.
//---------------------------------------------------------------------------------------
    public Plane(String flightNumber, String airlineName,
           double fuelRemaining, int overdue, int passengerNumber, 
           AIRPLANETYPE planeType, boolean isLanded) 
    {
        this.flightNumber = flightNumber;
        this.airlineName = airlineName;
        this.fuelRemaining = fuelRemaining;
        this.passengerNumber = passengerNumber;
        this.overdue = overdue;
        this.planeType = planeType;
        this.isLanded = isLanded;
    }
//---------------------------------------------------------------------------------------
//  Getters and Setters.
//---------------------------------------------------------------------------------------
    public String getAirlineName() 
    {
        return airlineName;
    }
    public void setAirlineName(String airlineName) 
    {
        this.airlineName = airlineName;
    }
    public void setOverdue(int overdue) 
    {
        this.overdue = overdue;
    }
    public int getOverdue()
    {
        return overdue;
    }
    public String getFlightNumber() 
    {
        return flightNumber;
    }
    public void setFlightNumber(String flightNumber) 
    {
        this.flightNumber = flightNumber;
    }
    public double getFuelRemaining() 
    {
        return fuelRemaining;
    }
    public void setFuelRemaining(double fuelRemaining) 
    {
        this.fuelRemaining = fuelRemaining;
    }
    public int getPassengerNumber() 
    {
        return passengerNumber;
    }
    public void setPassengerNumber(int passengerNumber) 
    {
        this.passengerNumber = passengerNumber;
    }
    public AIRPLANETYPE getPlaneType() 
    {
        return planeType;
    }
    public void setPlaneType(AIRPLANETYPE planeType) 
    {
        this.planeType = planeType;
    }
    public boolean isLanded() 
    {
        return isLanded;
    }
    public void setLanded(boolean isLanded)
    {
        this.isLanded = isLanded;
    }
    public double getLimitBeforeLand() 
    {
        return timeLimitBeforeLand;
    }

public void setTimeLimitBeforeLand(double timeLimitBeforeLand) 
{
this.timeLimitBeforeLand = timeLimitBeforeLand;
}

//---------------------------------------------------------------------------------------
//  CompareTo() used with the Comparable implementation.
//---------------------------------------------------------------------------------------
    public int compareTo(Object arg0) 
    {
        if((arg0 != null) && (arg0 instanceof Plane))
        {
        Plane p = (Plane) arg0;
        return (int)Math.ceil(this.timeLimitBeforeLand - p.getLimitBeforeLand());
        }
        return 0;
    }
//---------------------------------------------------------------------------------------
//  toString().
//---------------------------------------------------------------------------------------
    public String toString() 
    {
        return "Plane: flightNumber=" + flightNumber + "."
                + " airlineName=" + airlineName + "."
                + " fuelRemaining=" + fuelRemaining + " litres."
                + " overdue=" + overdue + " minutes."
                + " passengerNumber="+ passengerNumber + "."
                + " airplaneType=" + planeType +
                "hasLanded=" + isLanded+ ".\n";
    }
}
4

1 回答 1

0

in 的第二个参数Collections.sortComparator而不是Plane. 由于我没有看到 Comparator 的提及,您应该能够使用自然顺序(由 Plane 对象中的 compareTo 方法定义)并且在Collections.sort

编辑:除非您刚刚排除了该代码,否则您不会创建任何 Plane 实例,并且您在这里使用的是空集合...

   TreeMap<String, PlaneStore> map = new TreeMap<String, PlaneStore>();
   ArrayList<Plane> copyList = new ArrayList<Plane>(map.);

并且您将按 PlaneStores 进行排序,因此您必须获取Plane每个 PlaneStore 中的所有 s 并将它们添加到您的copyList排序之前。

我会考虑更多地研究每个集合,并确定最适合您需要的集合。

于 2012-12-14T18:52:28.457 回答