當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。