我是使用优先级队列的新手,并且格式化了此代码。我的优先级队列代码中有什么错误?

I am new with working with priority queues and have formatted this code wrong.What is the error in my Priority Queue code?

提问人:Samik Pandit 提问时间:9/8/2022 更新时间:10/16/2022 访问量:204

问:

输出框中的错误是: 线程“main”java.lang.NullPointerException 中的异常:无法分配字段“value”,因为“this.priorityqueue[this.count]”为 null 在 PriorityQueue.enQueue(PriorityQueue.java:16) 在 Main.main(Main.java:4)

它具有排队、取消排队、查看优先级队列等操作。 主要在“排队”部分显示错误。

public class PQ {
    public int value;
    public int priority;
}

public class PriorityQueue {
    public PQ[] priorityqueue;
    public int count;
    
    public PriorityQueue(int size){
        this.count = 0;
        this.priorityqueue = new PQ[size];
        System.out.println("The Priority Queue is create with the size of :" + size);
    }

    public void enQueue(int element,int priority){
        if (count == priorityqueue.length){
            System.out.println("Priority Queue Overflow!");
        }
        else {
            priorityqueue[count].value = element;
            priorityqueue[count].priority = priority;
            count++;
        }
    }
    
    public int peekprio(){
        int max = Integer.MIN_VALUE;
        int index = -1;
        for (int i = 0;i < count;i++){
            if (priorityqueue[i].priority > max){
                max = priorityqueue[i].priority;
                index = i;
            } else if (priorityqueue[i].priority == max && index > -1 && priorityqueue[index].value < priorityqueue[i].value){
                index = i;
            }
        }
        return index;
    }
    
    public int peek(){
        if (count == 0){
            System.out.println("Priority Queue Underflow!");
            return -1;
        }
        else {
            int index = -1;
            int max = Integer.MIN_VALUE;
            for (int i = 0; i < count; i++) {
                if (priorityqueue[i].priority > max) {
                    max = priorityqueue[i].priority;
                    index = i;
                }
                else if (priorityqueue[i].priority == max && index > -1 && priorityqueue[index].value < priorityqueue[i].value){
                    index = i;
                }
            }
            return priorityqueue[index].value;
        }
    }

    public void deQueue(){
        if (count == 0){
            System.out.println("Priority Queue Underflow!");
        }
        else {
            int element = priorityqueue[peekprio()].value;
            int index = peekprio();
            for (int i = index;i < count;i++){
                priorityqueue[i] = priorityqueue[i + 1];
            }
            count--;
            System.out.println("Value deQueued :" + element);
        }
    }
}
public class Main {
    public static void main(String[] args) {
        PriorityQueue pq = new PriorityQueue(5);
        pq.enQueue(1,0);
        pq.enQueue(3,3);
        pq.enQueue(5,5);
        pq.enQueue(2,2);
    }
}
java 异常 nullpointerexception 优先级队列

评论

2赞 Holger 9/8/2022
您永远不会创建任何对象,更不用说在数组中存储对象了。PQPQ

答:

0赞 re_arg 9/9/2022 #1

您正在使用对象数组在内部对优先级队列进行建模,但缺少 PQ 对象本身的创建,因此当您尝试为队列分配值和优先级时,它会尝试将其设置为尚不存在的对象。PQ

当你这样做时:在构造函数上,你只创建数组,当你想将它们添加到数组中时,你仍然需要创建单个对象。this.priorityqueue = new PQ[size];

为此,请更改方法上的此部分以添加:enQueuenew PQ()

else {
   priorityqueue[count] = new PQ();
   priorityqueue[count].value = element;
   priorityqueue[count].priority = priority;
   count++;
}

理想情况下,您希望在类上为其创建构造函数,而不是直接从外部设置属性。PQ

希望能有所帮助。