對於我的數據結構類,我們的任務是實現一個使用已創建的基於數組的隊列的PriorityQueue類。除了克隆方法外,一切都在PriorityQueue類中工作。當克隆方法被激活時,即使正在克隆的隊列中有數據,也不會返回任何內容。 PriorityQueue類使用ArrayQueues數組。我只是從我的類複製構造函數和克隆方法。PriorityQueue中的clone()方法實現
感謝您的幫助
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;
}
什麼是「什麼都沒有返回」的意思?隊列中爲空或空陣列? – 2011-04-18 00:54:57
當返回一個PriorityQueueArray對象時,實例變量manyItems會從被克隆的對象(5)中返回正確的數字,但是正在克隆的PriorityQueueArray中的項目(隊列[])不會被複制到答案變量中。 – Chad 2011-04-18 01:10:17