java - PriorityQueue中的clone()方法实现

标签 java priority-queue

对于我的数据结构类,我们的任务是实现一个 PriorityQueue 类,该类使用已创建的基于数组的队列。除了克隆方法之外,PriorityQueue 类中的所有内容都正常工作。当克隆方法被激活时,即使队列中有正在克隆的数据,也不会返回任何内容。 PriorityQueue 类使用 ArrayQueue 数组。我刚刚从我的类中复制了构造函数和克隆方法。

感谢您的帮助

private ArrayQueue<E>[] queues;
private int manyItems;
private int highest;

public PriorityQueueArray(int a_highest) {
    manyItems = 0;
    highest = a_highest;
    queues = new ArrayQueue[a_highest+1];
    for(int i = 0; i <= a_highest; i++) {
        queues[i] = new ArrayQueue();
    }
}

public PriorityQueueArray<E> clone() {
    PriorityQueueArray<E> answer;

    try{
        answer = (PriorityQueueArray<E>) super.clone();
    } catch (CloneNotSupportedException e) {
        // This exception should not occur. But if it does, it would probably indicate a
        // programming error that made super.clone unavailable. The most common error
        // The most common error would be forgetting the "Implements Cloneable"
        // clause at the start of this class.
        throw new RuntimeException
           ("This class does not implement Cloneable");         
    }
    for(int i = 0; i <= highest; i++) {
        answer.queues[i] = queues[i].clone();
    }   
    return answer;
}

最佳答案

尝试使用

@Override
public Object clone() throws CloneNotSupportedException {
  // your method here.
  ...
}

作为方法签名。看来您没有使用稍微不同的签名来正确覆盖克隆方法。请参阅information关于如何使用编译器来解决类似问题的 @Override 注释。

参见here有关克隆方法的更多详细信息。

关于java - PriorityQueue中的clone()方法实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5697524/

相关文章:

java - 未指定时间时,JAXB 将 dateTime 编码为空值

c++ - 并发可变优先级队列

java - 如何在java中实现有序但未排序的优先级队列?

java - 如何计算java中优先级队列中每次插入的中位数?

java - Date.getTime() 不包括时间?

java - 阻止应用程序在启动时暂停音乐(来自 Spotify 等)?

java - 有人可以向我解释间接堆/间接优先级队列的概念吗?

C++ 标准模板库优先级队列抛出异常,消息为 "Invalid Heap"

Java 从我的程序中获取奇怪的文本作为输出

java - 具有动态字段的 Spring Data Solr 不起作用