Java Deque element()方法

Java Deque element()方法

Deque接口element() 方法返回容器中最前面的元素。它不会删除容器中的元素。这个方法返回Deque的头部。当Deque为空时,该方法会抛出一个异常。

语法:

E element()

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

返回: 该方法返回容器前端或Deque头部的元素。

异常: 当Deque为空时,该函数会抛出NoSuchElementException。

以下程序说明了Deque的element()方法:

程序1: 借助 LinkedList

// Java Program Demonstrate element()
// method of Deque
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of Deque
        Deque<Integer> DQ
            = new LinkedList<Integer>();
 
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // print Deque
        System.out.println("Deque: " + DQ);
 
        // print head
        System.out.println("Deque's head: " + DQ.element());
    }
}

输出

Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642

程序2:ArrayDeque 的帮助下 。

// Java Program Demonstrate element()
// method of Deque
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of Deque
        Deque<Integer> DQ
            = new ArrayDeque<Integer>();
 
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // print Deque
        System.out.println("Deque: " + DQ);
 
        // print head
        System.out.println("Deque's head: " + DQ.element());
    }
}

输出

Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642

程序3:ConcurrentLinkedDeque 的帮助下 。

// Java Program Demonstrate element()
// method of Deque
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of Deque
        Deque<Integer> DQ
            = new ConcurrentLinkedDeque<Integer>();
 
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // print Deque
        System.out.println("Deque: " + DQ);
 
        // print head
        System.out.println("Deque's head: " + DQ.element());
    }
}

输出

Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642

程序4:LinkedBlockingDeque 的帮助下 。

// Java Program Demonstrate element()
// method of Deque
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of Deque
        Deque<Integer> DQ
            = new LinkedBlockingDeque<Integer>();
 
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // print Deque
        System.out.println("Deque: " + DQ);
 
        // print head
        System.out.println("Deque's head: " + DQ.element());
    }
}

输出

Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642

程序2

// Java Program Demonstrate element()
// method of Deque
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of Deque
        Deque<Integer> DQ
            = new LinkedList<Integer>();
 
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // print Deque
        System.out.println("Deque: " + DQ);
 
        // print head
        System.out.println("Deque's head: " + DQ.element());
 
        DQ.clear();
 
        // Deque is empty now hence exception
        System.out.println("Deque's head: " + DQ.element());
    }
}

输出

Exception in thread "main" java.util.NoSuchElementException
    at java.util.LinkedList.getFirst(LinkedList.java:244)
    at java.util.LinkedList.element(LinkedList.java:663)
    at GFG.main(GFG.java:29)

参考资料: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#element-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程