當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ConcurrentLinkedDeque pop()用法及代碼示例


Java中的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 peration "
                           + CLD); 
    } 
}
輸出:
Initial ConcurrentLinkedDeque: [Geeks, For, Geeks, To, Welcome]
Popped element: Geeks
Popped element: For
ConcurrentLinkedDeque after pop peration [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]


相關用法


注:本文由純淨天空篩選整理自MerlynShelley大神的英文原創作品 ConcurrentLinkedDeque pop() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。