📜  Java中的队列元素()方法

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

Java中的队列元素()方法

Queue 接口element()方法返回容器最前面的元素。它不会删除容器中的元素。此方法返回队列的头部。

此方法与 peek() 的不同之处仅在于如果此队列为空,它将引发异常。

句法:

E element()

返回:此方法返回队列的头部

异常:当队列为空并调用函数时,函数抛出NoSuchElementException

下面的程序说明了 Queue 的 element() 方法:

程序 1:LinkedList的帮助下。

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

程序 2:ArrayDeque的帮助下。

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

程序 3:LinkedBlockingDeque的帮助下。

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

程序 4:ConcurrentLinkedDeque的帮助下。

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

下面的程序说明了此方法引发的异常

程序 5:显示NoSuchElementException

// Java Program Demonstrate element()
// method of Queue
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Queue
        Queue Q
            = new LinkedList();
  
        // Add numbers to end of Queue
        Q.add(7855642);
        Q.add(35658786);
        Q.add(5278367);
        Q.add(74381793);
  
        // print queue
        System.out.println("Queue: " + Q);
  
        // print head
        System.out.println("Queue's head: " + Q.element());
  
        Q.clear();
  
        // print queue
        System.out.println("Queue: " + Q);
  
        try {
            // Queue is empty now hence exception
            System.out.println("Queue's head: " + Q.element());
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
输出:
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue: []
Exception: java.util.NoSuchElementException

参考: https: Java/util/Queue.html#element–