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 removeLastOccurrence()用法及代碼示例
- Java Deque remove()用法及代碼示例
- Java Deque removeFirstOccurrence()用法及代碼示例
- Java Deque removeFirst()用法及代碼示例
- Java Deque size()用法及代碼示例
- Java Deque pollLast()用法及代碼示例
- Java Deque addLast()用法及代碼示例
- Java Deque addAll()用法及代碼示例
- Java Deque poll()用法及代碼示例
- Java Deque pop()用法及代碼示例
- Java Deque pollFirst()用法及代碼示例
- Java Deque getLast()用法及代碼示例
- Java Deque getFirst()用法及代碼示例
- Java Deque iterator()用法及代碼示例
- Java Deque peek()用法及代碼示例
- Java Deque add()用法及代碼示例
- Java Deque offerFirst()用法及代碼示例
- Java Deque addFirst()用法及代碼示例
- Java Deque offer()用法及代碼示例
- Java Deque element()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Deque removeLast() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。