我制作了一個 QueueRunner 課程。我試圖找出在 poll() 或 Offer() 之后迭代到隊列的頭部,以使用 peek() 返回隊列的頭部。不過,我在返回隊列的頭部或前面時遇到了麻煩。Public class Queue<T> {private ArrayList<T> elements;public Queue() { this.elements = new ArrayList<T>();}/*** Offers an element to the end of the queue.** @param T item*/public void offer(T element) { this.elements.add(element);}/*** Peeks at, but does not remove, the element at the head of the queue.** @return T*/public T peek() { if(this.elements.size()==0) { return null; } else { return this.elements; // return this.elements.get(this.elements.size()-1); }}/*** Polls an element from the head of the queue.** @return T*/public T poll() { return this.elements.remove(0);}
添加回答
舉報
0/150
提交
取消