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


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


Java Deque 接口的 peekLast() 方法檢索而不是刪除給定雙端隊列的最後一個元素。但是,如果雙端隊列為空,則它可能返回 null。

用法:

E  peekLast()

參數:

NA

返回:

上述方法用於返回給定雙端隊列的尾部。另一方麵,如果雙端隊列為空,則該方法可能返回 null。

例子1

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepeekLastExample1 {
  public static void main(String[] args) {
	  Deque<Character> deque = new ArrayDeque<>();
	  deque.offer('J');
	  deque.offer('A');
	  deque.offer('V');
	  deque.offer('A');
	  Character val = deque.peekLast();
   System.out.println("The last character is given as:"+val);   
   System.out.println("The final deque is given as:");   
	for (Character character:deque) {
		System.out.println(character);
    	}
     }
  }

輸出:

The last character is given as:A
The final deque is given as:
J
A
V
A

例子2

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepeekLastExample2 {
   public static void main(String[] args) {
	   Deque<String> deque = new ArrayDeque<>();
		  deque.offer("Java");
		  deque.offer("is");
		  deque.offer("a");
		  deque.offer("secured");
		  deque.offer("language");
		  String val = deque.peekLast();
		System.out.println("The last string value is given as:"+val);			    
		System.out.println("The final deque is given as:"+deque);    
	     }
	  }

輸出:

The last string value is given as:language
The final deque is given as:[Java, is, a, secured, language]

例子3

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepeekLastExample3 {
   public static void main(String[] args) {
	   Deque<Integer> deque = new ArrayDeque<>();
		  deque.offer(1);
		  deque.offer(2);
		  deque.offer(3);
		  deque.offer(4);
		  deque.offer(5);
		  int val = deque.peekLast();
		System.out.println("The last integer value is given as:"+val); 
		System.out.println("The final deque is given as:");
		for (Integer integer:deque) {
			System.out.println(integer);
	     	}
	     }
	  }

輸出:

The last integer value is given as:5
The final deque is given as:
1
2
3
4
5




相關用法


注:本文由純淨天空篩選整理自 Java Deque peekLast() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。