Java ConcurrentLinkedDeque pop()方法及示例

Java ConcurrentLinkedDeque pop()方法及示例

Java .util.ConcurrentLinkedDeque.pop() 方法是用来从ConcurrentLinkedDeque中弹出一个元素。该元素从ConcurrentLinkedDeque的顶部弹出,并从同一位置移除。

语法:

ConcurrentLinkedDeque.pop()

参数: 该方法不接受任何参数。

返回值: 该方法返回存在于ConcurrentLinkedDeque顶部的元素,然后将其删除。

异常: 如果ConcurrentLinkedDeque为空,该方法会抛出EmptyConcurrentLinkedDequeException。

以下程序说明了Java.util.ConcurrentLinkedDeque.pop()方法:

程序1:

// Java code to illustrate pop()
 
import java.util.*;
import java.util.concurrent.*;
 
public class ConcurrentLinkedDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ConcurrentLinkedDeque
        ConcurrentLinkedDeque<String> CLD
            = new ConcurrentLinkedDeque<String>();
 
        // Use add() method to add elements
        CLD.push("Welcome");
        CLD.push("To");
        CLD.push("Geeks");
        CLD.push("For");
        CLD.push("Geeks");
 
        // Displaying the ConcurrentLinkedDeque
        System.out.println("Initial ConcurrentLinkedDeque: "
                           + CLD);
 
        // Removing elements using pop() method
        System.out.println("Popped element: " + CLD.pop());
        System.out.println("Popped element: " + CLD.pop());
 
        // Displaying the ConcurrentLinkedDeque after pop operation
        System.out.println("ConcurrentLinkedDeque after pop operation "
                           + CLD);
    }
}

输出

Initial ConcurrentLinkedDeque: [Geeks, For, Geeks, To, Welcome]
Popped element: Geeks
Popped element: For
ConcurrentLinkedDeque after pop operation [Geeks, To, Welcome]

代码2:

// Java code to illustrate pop()
 
import java.util.*;
import java.util.concurrent.*;
 
public class ConcurrentLinkedDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ConcurrentLinkedDeque
        ConcurrentLinkedDeque<Integer> CLD
            = new ConcurrentLinkedDeque<Integer>();
 
        // Use add() method to add elements
        CLD.push(10);
        CLD.push(15);
        CLD.push(30);
        CLD.push(20);
        CLD.push(5);
 
        // Displaying the ConcurrentLinkedDeque
        System.out.println("Initial ConcurrentLinkedDeque: "
                           + CLD);
 
        // Removing elements using pop() method
        System.out.println("Popped element: " + CLD.pop());
        System.out.println("Popped element: " + CLD.pop());
 
        // Displaying the ConcurrentLinkedDeque after pop operation
        System.out.println("ConcurrentLinkedDeque after pop operation "
                           + CLD);
    }
}

输出

Initial ConcurrentLinkedDeque: [5, 20, 30, 15, 10]
Popped element: 5
Popped element: 20
ConcurrentLinkedDeque after pop operation [30, 15, 10]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程