Deque 接口的 removeFirst() 方法用于检索和删除给定双端队列的第一个元素。上述方法与 pollFirst() 方法不同,如果给定的双端队列为空,则抛出异常。
用法:
public E removeFirst()
参数:
NA
返回:
上述方法用于返回给定双端队列的头部。
抛出:
NoSuchElementException- 如果给定的双端队列为空。
例子1
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveFirstExample1 {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.offer("Welcome");
deque.offer("to");
deque.offer("our");
deque.offer("site");
String val = deque.removeFirst();
System.out.println("The removed string is:"+val);
System.out.println("The resultant deque is:");
for (String string:deque) {
System.out.println(string);
}
}
}
输出:
The removed string is:Welcome The resultant deque is: to our site
例子2
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveFirstExample2 {
public static void main(String[] args) {
Deque<Integer> deque = new ArrayDeque<>();
deque.offer(66);
deque.offer(11);
deque.offer(33);
deque.offer(44);
int val = deque.removeFirst();
System.out.println("The removed integer value is:"+val);
System.out.println("The resultant deque is:");
for (Integer integer:deque) {
System.out.println(integer);
}
}
}
输出:
The removed integer value is:66 The resultant deque is: 11 33 44
例子3
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveFirstExample3 {
public static void main(String[] args) {
Deque<Long> deque = new ArrayDeque<>();
deque.offer(11l);
deque.offer(88l);
deque.offer(22l);
deque.offer(99l);
Long val = deque.removeFirst();
System.out.println("The removed long value is:"+val);
System.out.println("The resultant deque is:");
for (Long long1:deque) {
System.out.println(long1);
}
}
}
输出:
The removed long value is:11 The resultant deque is: 88 22 99
相关用法
- Java Deque removeFirstOccurrence()用法及代码示例
- Java Deque remove()用法及代码示例
- Java Deque removeLast()用法及代码示例
- Java Deque removeLastOccurrence()用法及代码示例
- Java Deque size()用法及代码示例
- Java Deque pollLast()用法及代码示例
- Java Deque addLast()用法及代码示例
- Java Deque addAll()用法及代码示例
- Java Deque poll()用法及代码示例
- Java Deque pop()用法及代码示例
- Java Deque pollFirst()用法及代码示例
- Java Deque getLast()用法及代码示例
- Java Deque getFirst()用法及代码示例
- Java Deque iterator()用法及代码示例
- Java Deque peek()用法及代码示例
- Java Deque add()用法及代码示例
- Java Deque offerFirst()用法及代码示例
- Java Deque addFirst()用法及代码示例
- Java Deque offer()用法及代码示例
- Java Deque element()用法及代码示例
注:本文由纯净天空筛选整理自 Java Deque removeFirst() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。