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


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


Java Deque 接口的 removeLast() 方法用于检索和删除给定双端队列的最后一个元素。上述方法与 pollLast() 方法不同,如果给定的双端队列为空,则抛出异常。

用法:

public E removeLast()

参数:

NA

返回:

上述方法用于返回给定双端队列的尾部。

抛出:

NoSuchElementException- 如果给定的双端队列为空。

例子1

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveLastExample1 {
    public static void main(String[] args) {
    Deque<String> deque = new ArrayDeque<>();
       deque.offer("Welcome");
       deque.offer("to");
       deque.offer("my");
       deque.offer("World");
     String val = deque.removeLast();
   System.out.println("The value that needs to be removed is:"+val);
    System.out.println("The final deque is:");
    for (String string:deque) {
		System.out.println(string);
	    }  	
    }
}

输出:

The value that needs to be removed is:World
The final deque is:
Welcome
to
my

例子2

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveLastExample2 {
    public static void main(String[] args) {
    Deque<Character> deque = new ArrayDeque<>();
       deque.offer('W');
       deque.offer('E');
       deque.offer('B');
       deque.offer('S');
       deque.offer('I');
       deque.offer('T');
       deque.offer('E');
     Character val = deque.removeLast();
   System.out.println("The value that needs to be removed is:"+val);
    System.out.println("The final deque is:");
    for (Character character:deque) {
		System.out.println(character);
	} 	
    }
}

输出:

The value that needs to be removed is:E
The final deque is:
W
E
B
S
I
T

例子3

import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeremoveLastExample3 {
    public static void main(String[] args) {
    Deque<Float> deque = new ArrayDeque<>();
       deque.offer(55.9F);
       deque.offer(22.5f);
       deque.offer(77f);
       deque.offer(98.7f);
   Float  val = deque.removeLast();
   System.out.println("The value that needs to be removed is:"+val);
    System.out.println("The final deque is:");
    for (Float float1:deque) {
	System.out.println(float1);	
	} 	
  }
}

输出:

The value that needs to be removed is:98.7
The final deque is:
55.9
22.5
77.0




相关用法


注:本文由纯净天空筛选整理自 Java Deque removeLast() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。