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 pollLast()用法及代碼示例
- Java Deque poll()用法及代碼示例
- Java Deque pollFirst()用法及代碼示例
- Java Deque peek()用法及代碼示例
- Java Deque peekLast()用法及代碼示例
- Java Deque peekFirst()用法及代碼示例
- Java Deque push()用法及代碼示例
- Java Deque size()用法及代碼示例
- Java Deque remove()用法及代碼示例
- Java Deque removeLast()用法及代碼示例
- Java Deque addLast()用法及代碼示例
- Java Deque addAll()用法及代碼示例
- Java Deque getLast()用法及代碼示例
- Java Deque getFirst()用法及代碼示例
- Java Deque iterator()用法及代碼示例
- Java Deque add()用法及代碼示例
- Java Deque removeFirstOccurrence()用法及代碼示例
- Java Deque offerFirst()用法及代碼示例
- Java Deque addFirst()用法及代碼示例
- Java Deque offer()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Deque pop() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。