Java Deque 接口的 remove() 方法用於檢索和移除 deque 的頭部。該方法可能與 poll() 方法不同,僅在一種情況下,如果給定的雙端隊列為空,它會拋出異常。
指定者:
在接口 Queue<E> 中刪除
用法:
public E remove()
參數:
NA
返回:
上述方法用於返回給定雙端隊列所代表的隊列頭。
拋出:
NoSuchElementException- 如果給定的雙端隊列為空。
例子1
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveExample1 {
public static void main(String[] args) {
Deque<Character> deque = new ArrayDeque<>();
deque.add('W');
deque.add('E');
deque.add('L');
deque.add('C');
deque.add('O');
deque.add('M');
deque.add('E');
Character val = deque.pop();
System.out.println("The first character which has been popped is:"+val);
System.out.println("The remaining deque is given as:");
for (Character character:deque) {
System.out.println(character);
}
}
}
輸出:
The first character which has been popped is:W The remaining deque is given as: E L C O M E
例子2
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveExample2 {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.add("Computer");
deque.add("Programming");
deque.add("Language");
String val = deque.pop();
System.out.println("The first string that has been popped is:"+val);
System.out.println("The remaining deque is given as:");
for (String string:deque) {
System.out.println(string);
}
}
}
輸出:
The first string that has been popped is:Computer The remaining deque is given as: Programming Language
例子3
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveExample3 {
public static void main(String[] args) {
Deque<Float> deque = new ArrayDeque<>();
deque.add(56f);
deque.add(24.9f);
deque.add(58f);
Float val = deque.pop();
System.out.println("The first float value that has been popped is:"+val);
System.out.println("The remaining deque is given as:");
for (Float float1:deque) {
System.out.println(float1);
}
}
}
輸出:
The first float value that has been popped is:56.0 The remaining deque is given as: 24.9 58.0
Java Deque remove() 方法
Java Deque 接口的 remove(Object o) 方法用於從雙端隊列中刪除給定元素的第一次出現。如果給定的雙端隊列沒有元素,則它保持不變。如果 deque 包含指定的元素,則上述方法返回 true。此外,此方法等效於 removeFirstOccurence(Object)。
指定者:
在接口 Collection<E> 中刪除
用法:
boolean remove(Object o)
參數:
上述方法隻需要一個參數:
- 需要移除的元素 'o'。
返回:
如果給定元素被刪除,則上述方法用於返回 true。
拋出:
ClassCastException- 如果給定元素的類與給定雙端隊列不兼容。
NullPointerException- 如果指定的元素已經為空並且給定的雙端隊列不允許空元素。
例子1
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveExample_1 {
public static void main(String[] args) {
Deque<Character> deque = new ArrayDeque<>();
deque.add('W');
deque.add('E');
deque.add('L');
deque.add('C');
deque.add('O');
deque.add('M');
deque.add('E');
Boolean val = deque.remove('M');
System.out.println("The character is present in the deque:"+val);
System.out.println("The remaining deque is given as:");
for (Character character:deque) {
System.out.println(character);
}
}
}
輸出:
The character is present in the deque:true The remaining deque is given as: W E L C O E
例子2
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveExample_2 {
public static void main(String[] args) {
Deque<Float> deque = new ArrayDeque<>();
deque.add(76f);
deque.add(89.9f);
deque.add(60.8f);
deque.add(45.1f);
Boolean val = deque.remove(60.9f);
System.out.println("The float value is present in the deque:"+val);
System.out.println("The remaining deque is given as:");
for (Float float1:deque) {
System.out.println(float1);
}
}
}
輸出:
The float value is present in the deque:false The remaining deque is given as: 76.0 89.9 60.8 45.1
例子3
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveExample_3 {
public static void main(String[] args) {
Deque<Long> deque = new ArrayDeque<>();
deque.add(67l);
deque.add(77l);
deque.add(50l);
deque.add(22l);
Boolean val = deque.remove(77l);
System.out.println("The long value is present in the deque:"+val);
System.out.println("The remaining deque is given as:");
for (Long long1:deque) {
System.out.println(long1);
}
}
}
輸出:
The long value is present in the deque:true The remaining deque is given as: 67 50 22
相關用法
- Java Deque removeLast()用法及代碼示例
- Java Deque removeFirstOccurrence()用法及代碼示例
- Java Deque removeLastOccurrence()用法及代碼示例
- 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 remove() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。