Java Deque 接口的 peekFirst() 方法检索而不是删除给定双端队列的第一个元素。但是,如果双端队列为空,则它可能返回 null。
用法:
public E peekFirst()
参数:
不适用
返回:
上述方法用于返回给定双端队列的头部。另一方面,如果双端队列为空,则该方法可能返回 null。
例子1
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepeekFirstExample1 {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.offer("Tina");
deque.offer("Shyam");
deque.offer("Isha");
deque.offer("Falguni");
String val = deque.peekFirst();
System.out.println("The first value is given as:"+val);
System.out.println("The deque is given as:"+deque);
}
}
输出:
The first value is given as:Tina The deque is given as:[Tina, Shyam, Isha, Falguni]
例子2
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepeekFirstExample2 {
public static void main(String[] args) {
Deque<Integer> deque = new ArrayDeque<>();
deque.offer(34);
deque.offer(43);
deque.offer(22);
deque.offer(55);
int val = deque.peekFirst();
System.out.println("The first integer value is given as:"+val);
System.out.println("The deque is given as:"+deque);
}
}
输出:
The first integer value is given as:34 The deque is given as:[34, 43, 22, 55]
例子3
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepeekFirstExample3 {
public static void main(String[] args) {
Deque<Character> deque = new ArrayDeque<>();
deque.offer('H');
deque.offer('E');
deque.offer('L');
deque.offer('L');
deque.offer('O');
Character val = deque.peekFirst();
System.out.println("The first characater value is given as:"+val);
System.out.println("The final deque is given as:");
for (Character character:deque) {
System.out.println(character);
}
}
}
输出:
The first characater value is given as:H The final deque is given as: H E L L O
相关用法
- Java Deque peek()用法及代码示例
- Java Deque peekLast()用法及代码示例
- Java Deque pollLast()用法及代码示例
- Java Deque poll()用法及代码示例
- Java Deque pop()用法及代码示例
- Java Deque pollFirst()用法及代码示例
- 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 peekFirst() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。