全屏
java.util.ArrayDeque.pollFirst() 方法检索并移除这个deque的第一个元素。如果此deque 队列为空返回null。
声明
以下是java.util.ArrayDeque.pollFirst()方法的声明public E pollFirst()
参数NA
返回值
此方法返回此deque队列的头部,或如果此deque队列为空返回null。
异常NA
例子
下面的示例演示java.util.ArrayDeque.pollFirst()方法的用法。package cn.sxt;
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 deque = new ArrayDeque(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.pollFirst();
System.out.println("Element removed is " + retval);
// printing all the elements available in deque after using pollFirst()
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
分享到:
0评论