📌  相关文章
📜  Java中的 ConcurrentLinkedDeque getFirst() 方法

📅  最后修改于: 2022-05-13 01:54:31.934000             🧑  作者: Mango

Java中的 ConcurrentLinkedDeque getFirst() 方法

Java.util.concurrent.ConcurrentLinkedDeque.getFirst() 方法是Java中的一个内置方法,它返回双端队列容器的第一个元素。

句法:

Conn_Linked_Deque.getFirst()

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

返回值:该方法返回出现在双端队列中的第一个元素。

异常:当双端队列为空时,函数会抛出NoSuchElementException

下面的程序说明了 ConcurrentLinkedDeque.getFirst() 方法:

程序 1

/* Java Program to Demonstrate getFirst()
   method of ConcurrentLinkedDeque */
  
import java.util.concurrent.*;
class GFG {
    public static void main(String[] args)
    {
  
        // Creating an empty Deque
        ConcurrentLinkedDeque cld = 
                    new ConcurrentLinkedDeque();
  
        // Add elements into the Deque
        cld.add("Welcome");
        cld.add("To");
        cld.add("Geeks");
        cld.add("4");
        cld.add("Geeks");
  
        // Displaying the Deque
        System.out.println("Elements in the Deque: " + cld);
  
        // Displaying the first element
        System.out.println("The first element is: " +
                                      cld.getFirst());
    }
}
输出:
Elements in the Deque: [Welcome, To, Geeks, 4, Geeks]
The first element is: Welcome

方案二:

/* Java Program to Demonstrate getFirst()
   method of ConcurrentLinkedDeque */
  
import java.util.concurrent.*;
class GFG {
    public static void main(String[] args)
    {
  
        // Creating an empty Deque
        ConcurrentLinkedDeque cld = 
                         new ConcurrentLinkedDeque();
  
        try {
            // Displaying the first element
            System.out.println("The first element "
                               + "is: " + cld.getFirst());
        }
        catch (Exception e) {
            System.out.println(e);
        }
  
        // Add elements into the Deque
        cld.add(12);
        cld.add(43);
        cld.add(29);
        cld.add(16);
        cld.add(70);
  
        // Displaying the Deque
        System.out.println("Elements in the Deque: " + cld);
  
        // Displaying the first element
        System.out.println("The first element is: " +
                                       cld.getFirst());
    }
}
输出:
java.util.NoSuchElementException
Elements in the Deque: [12, 43, 29, 16, 70]
The first element is: 12

参考: https: Java/util/concurrent/ConcurrentLinkedDeque.html#getFirst()