Java中的java.util.ArrayDeque.removeFirstOccurrence(Object)方法用於從此雙端隊列中刪除特定元素的首次出現。如果該元素僅存在一次,則將該元素刪除並返回true。如果沒有刪除此類元素,則返回false。
用法:
Array_Deque.removeFirstOccurrence(Object O)
參數:該方法采用ArrayDeque類型的一個參數O,表示要刪除其第一次出現的元素。
返回值:如果元素存在於Map中,則此方法返回true,否則返回false。
下麵的程序演示了Java.util.ArrayDeque.removeFirstOccurrence()方法:
程序1:
// Java code to illustrate removeFirstOccurrence()
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
Deque<String> de_que = new ArrayDeque<String>();
// Use add() method to add elements into the Deque
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("For");
de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("Initial ArrayDeque: " + de_que);
// Removing elements using removeFirstOccurrence() method
de_que.removeFirstOccurrence("Geeks");
de_que.removeFirstOccurrence("For");
// Displaying the ArrayDeque after removal
System.out.println("ArrayDeque after removing "
+ "elements: " + de_que);
}
}
輸出:
Initial ArrayDeque: [Welcome, To, Geeks, For, Geeks] ArrayDeque after removing elements: [Welcome, To, Geeks]
示例2:
// Java code to illustrate removeFirstOccurrence()
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
Deque<Integer> de_que = new ArrayDeque<Integer>();
// Use add() method to add elements into the Deque
de_que.add(10);
de_que.add(10);
de_que.add(30);
de_que.add(10);
de_que.add(30);
// Displaying the ArrayDeque
System.out.println("Initial ArrayDeque: " + de_que);
// Removing elements using removeFirstOccurrence() method
de_que.removeFirstOccurrence(30);
de_que.removeFirstOccurrence(5);
// Displaying the ArrayDeque after removal
System.out.println("ArrayDeque after removing "
+ "elements: " + de_que);
}
}
輸出:
Initial ArrayDeque: [10, 10, 30, 10, 30] ArrayDeque after removing elements: [10, 10, 10, 30]
相關用法
- Java LinkedBlockingDeque removeFirstOccurrence()用法及代碼示例
- Java LinkedList removeFirstOccurrence()用法及代碼示例
- Java ConcurrentLinkedDeque removeFirstOccurrence()用法及代碼示例
- Java BlockingDeque removeFirstOccurrence()用法及代碼示例
- Java ArrayDeque add()用法及代碼示例
- Java ArrayDeque contains()用法及代碼示例
- Java ArrayDeque pop()用法及代碼示例
- Java ArrayDeque peekFirst()用法及代碼示例
- Java ArrayDeque spliterator()用法及代碼示例
- Java ArrayDeque peek()用法及代碼示例
- Java ArrayDeque removeFirst()用法及代碼示例
- Java ArrayDeque removeLast()用法及代碼示例
- Java ArrayDeque offer()用法及代碼示例
- Java ArrayDeque pollLast()用法及代碼示例
- Java ArrayDeque iterator()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 ArrayDeque removeFirstOccurrence() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。