0

我正在将此文本文件输入到命令行中:

800    5    10  
800    1    8  
800    7    7  
810    2    9  
845    2    10  
850    1    3  

第一列是时间,然后是优先级,然后是长度
输出按时间加长度排序,如果该项的时间小于等于当前时间,则输出下一项,优先级最高的先(是它令人困惑,但它是一个类项目)

这将输出:

Job: [800,1,8] Start time: 800 End time:808  
Job: [800,5,10] Start time: 800 End time:818  
Job: [810,2,9] Start time: 810 End time: 819  
Job: [800,7,7] Start time: 800 End time: 807   
Job: [845,2,10] Start time: 845 End time 855   
Job: [850,1,3] Start time: 850 End time: 853     

我使用带有称为事件的队列方法的链表和称为队列的优先级队列
我的问题是while循环while (event.size() != 0 && queue.size() != 0)根本没有执行。
如果我将其更改为 do while 循环,我会在 while 上收到空指针异常错误(event.peek().time <= currentTime)if (event.peek().time > currentTime) 我已尝试通过添加来修复空指针异常event.peek() != null,但它仍然无法正常工作。事件(链表)中有 6 个作业对象,所以我不知道为什么 event.peek() 返回 null。

import java.util.*;
import java.io.*;

public class pj2
{   
    Queue<Job> event = new LinkedList<Job>();//interface queue
    PriorityQueue<Job> queue = new PriorityQueue<Job>();

    public static void main(String[] args) throws IOException
    {
        if (args.length != 1)
        {   
            System.out.println("Usage: java pj2 jobs.txt");
            System.exit(0);
        }
        else
            new pj2(args[0]);
    }

    public pj2 (String textFile) throws IOException
    {       
        File file = new File(textFile);
        if (!file.exists())
        {
            System.out.println(textFile + " does not exist.");
            System.exit(0);
        }

        //add time,priority,length to event queue
        Scanner data = new Scanner(file);
        while (data.hasNext())
        {
            int time = Integer.parseInt(data.next());
            int priority = Integer.parseInt(data.next());
            int length = Integer.parseInt(data.next());
            Job temp = new Job(time,priority,length);
            event.add(temp);
        }
        data.close();

        int currentTime = 0;
        //loop through priority queue, outputting results
        while (event.size() != 0 && queue.size() != 0)
        {
            if (queue.size() == 0)
                currentTime = event.peek().time;
            while (event.peek().time <= currentTime)
            {
                currentTime += event.peek().length;
                queue.offer(event.poll());
            }

            if (event.peek().time > currentTime)
            {
                currentTime = (event.peek().time + event.peek().length);
                queue.offer(event.poll());
            }

                System.out.println(queue.peek() + " Start time: " + queue.peek().time + " End time: " + (queue.peek().time + queue.peek().length));
                queue.poll();
        }

    }
}

public class Job implements Comparable<Job>
{
    int time, length, priority;

    public Job(int time, int priority, int length)
    {
        this.time = time;
        this.priority = priority;
        this.length = length;
    }
    public int compareTo(Job that)
    {
        if (this.priority == that.priority)
            return this.time - that.time;
        return this.priority - that.priority;
    }
    public String toString()
    {
        return "[" + time + "," + priority + "," + length + "]";
    }

}
4

2 回答 2

0

+1 上一张海报指向调试器教程的指针。如果您查看在评估循环条件时找到的值,您将能够立即发现此问题。(“哦,event.size() != 0 && queue.size() != 0 是假的,因为 queue.size() != 0 是假的。”)

Generally, your thought process for solving problems like this should be, "If the while loop is not executing, its condition is false. I need to find out which part of the condition is false. From there I can figure out whether the bug is 'the previous part of my program is making this false where it should be true', or 'this shouldn't actually be part of the loop condition'."

In this case, you require event.size() != 0 AND queue.size() != 0, but you don't add anything to queue prior to the loop. I think you need to rethink your loop conditions.

于 2010-01-30T08:38:54.160 回答
0

While reading the input file you fill the event list, but filling the queue is left to the while loop, so your condition:

    while (event.size() != 0 && queue.size() != 0)

never will be true as the queue is still empty. So either you need to change your loop condition to:

    while (event.size() != 0 || queue.size() != 0)

to handle information as long as either the list or the queue still holds data, or you might have to split your loop into 2 loops, one to fill the queue from the list followed by one that uses the queue to determine your answer.

于 2010-01-30T08:45:23.193 回答