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


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


Java Deque 接口的 pop() 方法用于从此双端队列表示的给定堆栈中弹出一个元素。我们也可以说上面的方法移除并返回了双端队列的第一个元素。上述方法类似于 removeFirst() 方法。

用法:

public E pop()

参数:

NA

返回:

上述方法用于返回给定双端队列前面的元素。

抛出:

NoSuchElementException- 如果给定的双端队列为空。

例子1

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepopExample1 {
    public static void main(String[] args) {
		Deque<String> deque = new ArrayDeque<>();
	    deque.offer("Apple");
	    deque.offer("Guava");
	    deque.offer("Mango");
	    deque.offer("Pear");
	   String val =  deque.pop();
	  System.out.println("The first fruit which has been removed is:"+val);   
	  System.out.println("The final deque is given as:");   
	  for (String string:deque) {
		System.out.println(string);
	  }
   }
}

输出:

The first fruit which has been removed is:Apple
The final deque is given as:
Guava
Mango
Pear

例子2

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepopExample2 {
    public static void main(String[] args) {
		Deque<Float> deque = new ArrayDeque<>();
	    deque.offer(67f);
	    deque.offer(55f);
	    deque.offer(78f);
	    deque.offer(77f);
	   Float val =  deque.pop();
	  System.out.println("The first number which has been removed is:"+val);   
	  System.out.println("The final deque is given as:");   
	  for (Float float1:deque) {
		System.out.println(float1);
	}
   }
}

输出:

The first number which has been removed is:67.0
The final deque is given as:
55.0
78.0
77.0

例子3

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepopExample3 {
    public static void main(String[] args) {
		Deque<Character> deque = new ArrayDeque<>();
	    deque.offer('a');
	    deque.offer('b');
	    deque.offer('c');
	    deque.offer('d');
	   Character val =  deque.pop();
	  System.out.println("The first character which has been removed is:"+val);   
	  System.out.println("The final deque is given as:");   
	  for (Character character:deque) {
		System.out.println(character);
	}
   }
}

输出:

The first character which has been removed is:a
The final deque is given as:
b
c
d




相关用法


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