当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Deque poll()用法及代码示例


Java Deque 接口的 poll() 方法用于检索和移除 deque 的头部。另一方面,如果双端队列为空,则该方法可能返回 null。上述方法等价于pollFirst()。

指定者:

在接口 Queue<E> 中轮询

用法:

public E poll()

参数:

NA

返回:

上述方法用于返回给定双端队列的第一个元素。否则,如果双端队列为空,该方法可能会返回空值。

例子1

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepollExample1 {
   public static void main(String[] args) {
	   Deque<Float> deque = new ArrayDeque<>();
	    deque.offerLast(45.6f);
		deque.offerLast(23f);
		deque.offerLast(45.8f);
		deque.offerLast(66.9f);
		Float val = deque.poll();
		System.out.println("The first element is:"+val);
		System.out.println("The final deque is given as:"+deque);
    }
}

输出:

The first element is:45.6
The final deque is given as:[23.0, 45.8, 66.9]

例子2

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepollExample2 {
  public static void main(String[] args) {
	Deque<String> deque = new ArrayDeque<>();
	deque.offer("Java");
	deque.offer("is");	
	deque.offer("a");
	deque.offer("portable");
	deque.offer("language");
	String val = deque.poll();
   System.out.println("The first string value is:"+val);
   System.out.println("The final deque is given as:");
   for (String string:deque) {
	System.out.println(string);
    }
  }
}

输出:

The first string value is:Java
The final deque is given as:
is
a
portable
language

例子3

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepollExample3 {
  public static void main(String[] args) {
	Deque<Integer> deque = new ArrayDeque<>();
	deque.offer(34);
	deque.offer(65);
	deque.offer(59);
	deque.offer(95);
	deque.offer(68);
	int val = deque.poll();
   System.out.println("The first string value is:"+val);
   System.out.println("The final deque is given as:");
  for (Integer integer:deque) {
	System.out.println(integer);
    }
  }
}

输出:

The first string value is:34
The final deque is given as:
65
59
95
68




相关用法


注:本文由纯净天空筛选整理自 Java Deque poll() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。