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


Java Deque peekFirst()用法及代碼示例


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