我制作了一个 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
提交
取消