java.util.ArrayDeque.poll() 檢索並移除此queue佇列表示的佇列的頭部。返回null如果此queue佇列為空。
以下是java.util.ArrayDeque.poll()方法的宣告
public E poll()
NA
此方法返回此deque佇列表示的佇列的頭部,或null,如果此deque佇列為空。
NA
下面的範例演示java.util.ArrayDeque.poll()方法的用法。
package com.yiibai; import java.util.ArrayDeque; import java.util.Deque; public class ArrayDequeDemo { public static void main(String[] args) { // create an empty array deque with an initial capacity Deque<Integer> deque = new ArrayDeque<Integer>(8); // use add() method to add elements in the deque deque.add(25); deque.add(30); deque.add(20); deque.add(18); // printing all the elements available in deque for (Integer number : deque) { System.out.println("Number = " + number); } int retval = deque.poll(); System.out.println("Element removed is " + retval); // printing all the elements available in deque after using poll() for (Integer number : deque) { System.out.println("Number = " + number); } } }
讓我們來編譯和執行上面的程式,這將產生以下結果:
Number = 25 Number = 30 Number = 20 Number = 18 Element removed is 25 Number = 30 Number = 20 Number = 18