当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Deque remove()用法及代码示例


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)

参数:

上述方法只需要一个参数:

  1. 需要移除的元素 '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 remove() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。