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


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


Java Deque 接口的 pollLast() 方法用於檢索和刪除 deque 的最後一個元素。如果給定的雙端隊列為空,則該方法可能返回 null。

用法:

public E pollLast()

參數:

NA

返回:

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

例子1

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepollLastExample1 {
	public static void main(String[] args) {
		   Deque<String> deque = new ArrayDeque<>();
			deque.offer("Hello");
			deque.offer(" ");
			deque.offer("World");
			String val = deque.pollLast();
			System.out.println("The last string is given as:"+val);
			System.out.println("The remaining deque is:"+deque);
		}
}

輸出:

The last string is given as:World
The remaining deque is:[Hello,  ]

例子2

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepollLastExample2 {
	public static void main(String[] args) {
		   Deque<Float> deque = new ArrayDeque<>();
			deque.offer(24f);
			deque.offer(56.9f);
			deque.offer(98f);
			Float val = deque.pollLast();
			System.out.println("The last float value is given as:"+val);
			System.out.println("The remaining deque is:"+deque);
		}
}

輸出:

The last float value is given as:98.0
The remaining deque is:[24.0, 56.9]

例子3

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequepollLastExample3 {
	public static void main(String[] args) {
		   Deque<Character> deque = new ArrayDeque<>();
			  deque.offer('I');
			  deque.offer('N');
			  deque.offer('D');
			  deque.offer('I');
			  deque.offer('A');
			  deque.offer('N');
			  deque.offer('S');
			  Character val = deque.pollLast();		  
		System.out.println("The LAST character value is given as:"+val);
			System.out.println("The final deque is given as:");    
			for (Character character:deque) {
				System.out.println(character);
		    	}
		     }
		  }

輸出:

The LAST character value is given as:S
The final deque is given as:
I
N
D
I
A
N




相關用法


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