java.util.ArrayDeque.pollLast() 檢索並移除此deque佇列的最後一個元素。如果此deque佇列為空返回null。
以下是java.util.ArrayDeque.pollLast()方法的宣告
public E pollLast()
NA
此方法返回此deque佇列的尾部,或者null,如果此deque佇列為空。
NA
下面的範例演示java.util.ArrayDeque.pollLast()方法的用法。
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.pollLast(); System.out.println("Element removed is " + retval); // printing all the elements available in deque after using pollLast() for (Integer number : deque) { System.out.println("Number = " + number); } } }
讓我們來編譯和執行上面的程式,這將產生以下結果:
Number = 25 Number = 30 Number = 20 Number = 18 Element removed is 18 Number = 25 Number = 30 Number = 20