对于我的数据结构类,我们的任务是实现一个 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/