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


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


Java Deque 接口的 removeLast() 方法用於檢索和刪除給定雙端隊列的最後一個元素。上述方法與 pollLast() 方法不同,如果給定的雙端隊列為空,則拋出異常。

用法:

public E removeLast()

參數:

NA

返回:

上述方法用於返回給定雙端隊列的尾部。

拋出:

NoSuchElementException- 如果給定的雙端隊列為空。

例子1

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveLastExample1 {
    public static void main(String[] args) {
    Deque<String> deque = new ArrayDeque<>();
       deque.offer("Welcome");
       deque.offer("to");
       deque.offer("my");
       deque.offer("World");
     String val = deque.removeLast();
   System.out.println("The value that needs to be removed is:"+val);
    System.out.println("The final deque is:");
    for (String string:deque) {
		System.out.println(string);
	    }  	
    }
}

輸出:

The value that needs to be removed is:World
The final deque is:
Welcome
to
my

例子2

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveLastExample2 {
    public static void main(String[] args) {
    Deque<Character> deque = new ArrayDeque<>();
       deque.offer('W');
       deque.offer('E');
       deque.offer('B');
       deque.offer('S');
       deque.offer('I');
       deque.offer('T');
       deque.offer('E');
     Character val = deque.removeLast();
   System.out.println("The value that needs to be removed is:"+val);
    System.out.println("The final deque is:");
    for (Character character:deque) {
		System.out.println(character);
	} 	
    }
}

輸出:

The value that needs to be removed is:E
The final deque is:
W
E
B
S
I
T

例子3

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveLastExample3 {
    public static void main(String[] args) {
    Deque<Float> deque = new ArrayDeque<>();
       deque.offer(55.9F);
       deque.offer(22.5f);
       deque.offer(77f);
       deque.offer(98.7f);
   Float  val = deque.removeLast();
   System.out.println("The value that needs to be removed is:"+val);
    System.out.println("The final deque is:");
    for (Float float1:deque) {
	System.out.println(float1);	
	} 	
  }
}

輸出:

The value that needs to be removed is:98.7
The final deque is:
55.9
22.5
77.0




相關用法


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