当前位置: 首页 > 知识库问答 >
问题:

PriorityQueue未“轮询”优先级对象或我在compareTo方法中有逻辑错误

沈子昂
2023-03-14
scheduledTime|eventType|flightIdentifier|runwayUsed
00:01|ARRIVAL|A001|null
00:00|DEPARTURE|D001|null
00:01|DEPARTURE|D002|null
00:00|DEPARTURE|D003|null
 00:01|ARRIVAL|A001| 1
 00:00|DEPARTURE|D001| 4 
 00:01|DEPARTURE|D002| 2 
 00:00|DEPARTURE|D003| 3
00:01|ARRIVAL|A001| 1
00:00|DEPARTURE|D001| 2
00:01|DEPARTURE|D002| 3
00:00|DEPARTURE|D003| 4
public class Runway implements Comparable<Runway>{

//instance or class variables

private LocalTime whenAvailable; //when the runway is available
private LocalTime actualTime; //actual time the plane arrived or departed
private List<Flight> flights; //the flights that have been assigned to the runway
private Integer runwayNumber; // the number of the runway. for ex., 1 = Runway 1.
private LocalTime previousSchedTime; //the most recent previous time that the runway was scheduled for arrival or departure. This is just used
// for testing purposes.

/**
 * Constructor
 */
protected Runway() {
    whenAvailable = null;
    flights = new ArrayList<Flight>();
} 



/**
 * Determine if the runway is available
 * @param currentTime The scheduled time of the flight
 * @return true if the runway is available or false if it is not
 */
protected boolean isAvailable(LocalTime currentTime) {
    if(currentTime == null) {
        throw new IllegalArgumentException();
    } 

    return whenAvailable == null ||  !currentTime.isBefore(whenAvailable); // currentTime >= whenAvailable

} 



/**
 * Assign flight to the runway, i.e., set the actual time a flight uses the runway and the time ruwnay will be available again
 * @param flight The flight being assigned to the runway
 * @param scheduledTime The scheduled time of the flight
 * @param reserveTime The arrival or departure reserve times of the flight
 */
protected void assignFlight(Flight flight, LocalTime scheduledTime, int reserveTime) {

    //if the runway is available
    if(isAvailable(scheduledTime)) {


        //Set the actual time of the flight and when the runway will be available again

        //if the runway is null and available
        if(whenAvailable == null) { 
            actualTime = scheduledTime;
            whenAvailable = scheduledTime.plusMinutes(reserveTime);
        } 

        //if runway is available and the scheduled time of flight is equal to when the runway is available
        else if(scheduledTime == whenAvailable || scheduledTime.isBefore(whenAvailable)) {
            actualTime = whenAvailable;
            whenAvailable = actualTime.plusMinutes(reserveTime);
        } 




        else { //if scheduledTime > whenAvailable 


            actualTime = scheduledTime;
            whenAvailable = actualTime.plusMinutes(reserveTime);



        } 
    } 

    //if the runway is not available aka currentTime < whenAvailable
    else {
        actualTime = whenAvailable;
        whenAvailable = actualTime.plusMinutes(reserveTime);
    } 

    flights.add(flight);
    previousSchedTime = scheduledTime; //update previousSchedTime to scheduledTime



} 



/**
 * 
 * @return acutialTime the runway was used by flight
 */
protected LocalTime getActualTimeRunway() {
    return actualTime;
} 

/**
 * 
 * @return the time the runway is available
 */
protected LocalTime getWhenAvailable() {
    return whenAvailable;
} 


/**
 * 
 * @return List of all the flights that used the runway 
 */
protected List<Flight> getFlights(){

    List<Flight> tmpList = new LinkedList<Flight>();
    for(Flight f : flights) {
        tmpList.add(f);
    } 
    return tmpList;
} 


/**
 * 
 * @param runwayNumber set the number of the runway
 */
protected void setRunwayNumber(int runwayNumber) {
    if(runwayNumber < 1) {
        throw new IllegalArgumentException();
    } //end of if
    this.runwayNumber = runwayNumber;
} 

/**
 * 
 * @return the number of the runway
 */
protected Integer getRunwayNumber() {
    return runwayNumber;
} 


/**
 * 
 * @return the most recent previous time that the runway was scheduled for arrival or departure. This is used
// as a condition in the assignFlight method below
 */
protected LocalTime getPreviousSchedTime() {
    return previousSchedTime;
} 

/**
 * NOTE: this is intended to only be used for testing in other classes when used with reflection
 * @param previousSchedTime sets the previousScedTime
 */
private void setPreviousSchedTime(LocalTime previousSchedTime) {
    this.previousSchedTime = previousSchedTime;
} 


/**
 * Override compareTo method of Comparable interface
 * Set priority of runway instance when compared other runway instances
 */
@Override
public int compareTo(Runway other) {
    if(this.getWhenAvailable() == null && other.getWhenAvailable() == null) {
        return 0;
    } 
    else if(this.getWhenAvailable() == null && other.getWhenAvailable() != null) {
        return -1;
    } 
    else if(this.getWhenAvailable() !=null && other.getWhenAvailable() == null) {
        return 1; 
    } 
    else if(this.getWhenAvailable() !=null && other.getWhenAvailable() != null) {
        if(this.getWhenAvailable().equals(other.getWhenAvailable())) {
            return 0;
        } 
        else if(this.getWhenAvailable().isBefore(other.getWhenAvailable())) {
            return -1;
        } 
        else if(this.getWhenAvailable().isAfter(other.getWhenAvailable())) {
            return 1;
        } 
    } 

    return 0;
} 

/**
 * Intended use is only for JUnit testing when used with reflection
 * @param wA set whenAvailable time
 */
private void setWhenAvailable(LocalTime wA) {
    this.whenAvailable = wA;
} 
public class stackExchange {


public static void main(String[] args) {

    Flight f1 = new Flight("00:01", "ARRIVAL","A001");
    Flight f2 = new Flight("00:00", "DEPARTURE","D001");
    Flight f3 = new Flight("00:01", "DEPARTURE","D002");
    Flight f4 = new Flight("00:00", "DEPARTURE","D003");


    PriorityQueue<Flight> flightsPQ = new PriorityQueue<Flight>();
    flightsPQ.add(f1);
    flightsPQ.add(f2);
    flightsPQ.add(f3);
    flightsPQ.add(f4);

    Runway r1 = new Runway();
    r1.setRunwayNumber(1);

    Runway r2 = new Runway();
    r2.setRunwayNumber(2);

    Runway r3 = new Runway();
    r3.setRunwayNumber(3);

    Runway r4 = new Runway();
    r4.setRunwayNumber(4);

    PriorityQueue<Runway> runwaysPQ = new PriorityQueue<Runway>();
    runwaysPQ.add(r1);
    runwaysPQ.add(r2);
    runwaysPQ.add(r3);
    runwaysPQ.add(r4);

    while(!flightsPQ.isEmpty()) {
        Flight tmpFlight = flightsPQ.poll(); //remove priority flight from flightsPQ

        Runway tmpRunway = runwaysPQ.poll(); //remove priority runway from runwaysPQ
        tmpRunway.assignFlight(tmpFlight, tmpFlight.getScheduledTime(), tmpFlight.getReserveTime()); //assign the priority flight to the runwy
        tmpFlight.setActualTime(tmpRunway.getActualTimeRunway()); //set the actual time the flight was used
        tmpFlight.setRunwayUsed(tmpRunway); //tell the flight which runway it used

        //print out the flight data that used the runway and the number of the runway used
        //format: scheduledTime of flight | eventType | identifier | actualTime the flight used the runway | the number of the runway used (used to distinguish runway over
         // other runways
        System.out.println(tmpFlight.getScheduledTime() + "|" + tmpFlight.getEvent() + "|" + tmpFlight.getIdent() + "|" + tmpFlight.getActualTime()
                + "|" + tmpFlight.getRunwayUsed().getRunwayNumber());

        runwaysPQ.add(tmpRunway); //add the runway back into runwaysPQ
    } 





} //end of main

共有1个答案

颛孙昆
2023-03-14

在开始时,whenavailable为空,因此所有跑道都具有相同的优先级。根据文档:

这个队列的头是相对于指定顺序最少的元素。如果多个元素被绑定为最小值,那么头部就是这些元素中的一个--绑定被任意断开。

因此,如果你想要一致的订购,你应该首先比较供货时间,然后再比较跑道的数量。

 类似资料:
  • 我知道PriorityQueues的迭代器没有返回正确的顺序,因此我查看顺序的能力受到限制--但是我可以看到元素离开队列的顺序,而且它显然没有按照我希望的路径运行。 建议?

  • 问题内容: 下面的两个语句是否等效? 和 我可以使用某种真值表来验证这一点吗? 问题答案: 优先于,因此,即使 与…不同 因为那将被执行为 并且想要使它们相同的是以下内容(使用括号覆盖优先级规则): 这是一个示例说明:

  • 为什么JVM不遵守它自己的规则。以下面的例子为例。 结果是: 而实际结果应该是,根据&运算符是在运算符之前求值的事实: 对于为什么没有正确实现这一点,有一些解释会很好。即使在第二部分周围添加括号,也没有使用正确的优先级。

  • 最近我发现了这样一个片段: 它产生输出。通过查看运算符优先级表,我看到逻辑运算符和

  • 问题内容: 下面的两个语句是否等效? 和 我可以使用某种真值表来验证这一点吗? 问题答案: 优先于,因此,即使 与…不同 因为那将被执行为 并且想要使它们相同,是以下内容(使用括号覆盖优先级规则): 这是一个示例说明:

  • 本文向大家介绍oracle逻辑运算符与其优先级简介,包括了oracle逻辑运算符与其优先级简介的使用技巧和注意事项,需要的朋友参考一下 Oracle的逻辑运算符也是用在SQL语句中必不可少的因素,一共有三个 逻辑运算符 意义 and 双值运算符,如果左右两个条件都为真,则得到的值就为真 or 双值运算符,只要左右两个条件有一个为真,则得到的值就为真 not 单指运算符,如果原条件为真,则得到真,如