【发布时间】:2019-06-23 22:00:33
【问题描述】:
为什么“结果”是用简单的“T”创建的,而“temp”是用“Queue<T>”创建的,有关系吗?
// returns the item at the front of the given queue,
without
// removing it from the queue
public static <T> T peek(Queue<T> q)
throws NoSuchElementException {
/** COMPLETE THIS METHOD **/
if (q.isEmpty()) {
throw new NoSuchElementException("Queue Empty");
}
T result = q.dequeue();
Queue<T> temp = new Queue<T>();
temp.enqueue(result);
while(!q.isEmpty()) {
temp.enqueue(q.dequeue());
}
while(!temp.isEmpty()) {
q.enqueue(temp.dequeue());
}
return result;
}
【问题讨论】: