Deque 接口的 removeLastOccurrence() 方法用于从给定的双端队列中删除最后一次出现的指定元素。如果双端队列中没有元素,则它保持不变。如果指定的元素存在于双端队列中,则上述方法返回 true。
用法:
boolean removeLastOccurrence(Object o)
参数:
以上只需要一个参数:
- 需要从给定双端队列中删除的元素 'o'。
返回:
上述方法用于在指定元素被移除时返回true。
抛出:
- ClassCastException- 如果指定的元素与给定的双端队列不兼容。
- NullPointerException - 如果指定的元素已经为空并且给定的双端队列不允许空元素。
例子1
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveLastOccurrenceExample1 {
public static void main(String[] args) {
Deque<Character> deque = new ArrayDeque<>();
deque.push('a');
deque.push('d');
deque.push('c');
deque.push('b');
deque.push('a');
boolean val = deque.removeLastOccurrence('a');
System.out.println("The value is already present in the deque:"+val);
System.out.println("The final deque is given as:");
for (Character character:deque) {
System.out.println(character);
}
}
}
输出:
The value is already present in the deque:true The final deque is given as: a b c d
例子2
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveLastOccurrenceExample2 {
public static void main(String[] args) {
Deque<Long> deque = new ArrayDeque<>();
deque.add(23l);
deque.add(45l);
deque.add(63l);
deque.add(45l);
boolean val = deque.removeLastOccurrence(55l);
System.out.println("The value is already present in the deque:"+val);
System.out.println("The final deque is given as:");
for (Long long1:deque) {
System.out.println(long1);
}
}
}
输出:
The value is already present in the deque:false The final deque is given as: 23 45 63 45
例子3
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveLastOccurrenceExample3 {
public static void main(String[] args) {
Deque<Float> deque = new ArrayDeque<>();
deque.add(55.8f);
deque.add(22.3f);
deque.add(89f);
deque.add(22.3f);
boolean val = deque.removeLastOccurrence(22.3f);
System.out.println("The value is already present in the deque:"+val);
System.out.println("The final deque is given as:");
for (Float float1:deque) {
System.out.println(float1);
}
}
}
输出:
The value is already present in the deque:true The final deque is given as: 55.8 22.3 89.0
相关用法
- Java Deque removeLast()用法及代码示例
- 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 removeLastOccurrence() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。