Deque 接口的 removeFirstOccurrence() 方法用於從雙端隊列中刪除給定元素的第一次出現。如果雙端隊列中沒有元素,則它保持不變。如果 deque 包含指定的元素,它可能返回 true。
用法:
boolean removeFirstOccurrence(Object o)
參數:
以上隻需要一個參數:
- 需要從給定雙端隊列中刪除 d 的元素 'o'。
返回:
如果從雙端隊列中刪除特定元素,則上述方法返回 true。
拋出:
- ClassCastException- 如果給定元素的類與給定雙端隊列不兼容。
- NullPointerException - 如果指定的元素已經為空並且給定的雙端隊列不允許空元素。
例子1
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveFirstoccurrenceExample1 {
public static void main(String[] args) {
Deque<Character> deque = new ArrayDeque<>();
deque.push('d');
deque.push('c');
deque.push('b');
deque.push('a');
Boolean val = deque.removeFirstOccurrence('c');
System.out.println("The character is already present? "+val);
System.out.println("The final deque is given as:");
for (Character character:deque) {
System.out.println(character);
}
}
}
輸出:
The character is already present? true The final deque is given as: a b d
例子2
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveFirstoccurrenceExample2 {
public static void main(String[] args) {
Deque<Integer> deque = new ArrayDeque<>();
deque.push(2);
deque.push(5);
deque.push(6);
deque.push(7);
Boolean val = deque.removeFirstOccurrence(5);
System.out.println("The intger value is already preesnt? "+val);
System.out.println("The final deque is given as:");
for (Integer integer:deque) {
System.out.println(integer);
}
}
}
輸出:
The integer value is already preesnt? true The final deque is given as: 7 6 2
例子3
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveFirstoccurrenceExample3 {
public static void main(String[] args) {
Deque<Float> deque = new ArrayDeque<>();
deque.push(66.8f);
deque.push(50.8f);
deque.push(63.4f);
deque.push(71.2f);
Boolean val = deque.removeFirstOccurrence(5);
System.out.println("The float value is already preesnt? "+val);
System.out.println("The final deque is given as:");
for (Float float1:deque) {
System.out.println(float1);
}
}
}
輸出:
The float value is already preesnt? false The final deque is given as: 71.2 63.4 50.8 66.8
相關用法
- Java Deque removeFirst()用法及代碼示例
- Java Deque remove()用法及代碼示例
- Java Deque removeLast()用法及代碼示例
- Java Deque removeLastOccurrence()用法及代碼示例
- 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 removeFirstOccurrence() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。