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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。