Java Deque offer()方法

Java Deque offer()方法

Deque接口offer(E e) 方法在不违反容量限制的情况下可以立即插入指定的元素到这个Deque中。这个方法比add()方法更好,因为这个方法在容器容量满的时候不会抛出一个异常,因为它返回false。

语法

boolean offer(E e)

参数: 该方法接受一个强制性参数 e ,即要插入Deque中的元素。

返回: 该方法在成功插入时返回true,否则返回false。

异常: 该函数抛出四个异常,描述如下。

  • ClassCastException :当要输入的元素的类别阻止它被添加到这个容器中。
  • IllegalArgumentException :当元素的某些属性阻止它被添加到Deque中。
  • NullPointerException :当要插入的元素被传递为空,而Deque的接口不允许空元素。

下面的程序说明了Deque的offer()方法。

程序1:LinkedList 的帮助下 。

// Java Program Demonstrate offer()
// method of Deque when Null is passed
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Deque
        Deque<Integer> DQ
            = new LinkedList<Integer>();
  
        if (DQ.offer(10))
            System.out.println("The Deque is not full and 10 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(15))
            System.out.println("The Deque is not full and 15 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(25))
            System.out.println("The Deque is not full and 25 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(20))
            System.out.println("The Deque is not full and 20 is inserted");
        else
            System.out.println("The Deque is full");
  
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}

输出:

The Deque is not full and 10 is inserted
The Deque is not full and 15 is inserted
The Deque is not full and 25 is inserted
The Deque is not full and 20 is inserted
Deque: [10, 15, 25, 20]

程序2:ArrayDeque 的帮助下 。

// Java Program Demonstrate offer()
// method of Deque when Null is passed
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Deque
        Deque<Integer> DQ
            = new ArrayDeque<Integer>();
  
        if (DQ.offer(10))
            System.out.println("The Deque is not full and 10 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(15))
            System.out.println("The Deque is not full and 15 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(25))
            System.out.println("The Deque is not full and 25 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(20))
            System.out.println("The Deque is not full and 20 is inserted");
        else
            System.out.println("The Deque is full");
  
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}

输出:

The Deque is not full and 10 is inserted
The Deque is not full and 15 is inserted
The Deque is not full and 25 is inserted
The Deque is not full and 20 is inserted
Deque: [10, 15, 25, 20]

程序3:ConcurrentLinkedDeque 的帮助下 。

// Java Program Demonstrate offer()
// method of Deque when Null is passed
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>();
  
        if (DQ.offer(10))
            System.out.println("The Deque is not full and 10 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(15))
            System.out.println("The Deque is not full and 15 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(25))
            System.out.println("The Deque is not full and 25 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(20))
            System.out.println("The Deque is not full and 20 is inserted");
        else
            System.out.println("The Deque is full");
  
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}

输出:

The Deque is not full and 10 is inserted
The Deque is not full and 15 is inserted
The Deque is not full and 25 is inserted
The Deque is not full and 20 is inserted
Deque: [10, 15, 25, 20]

程序4:LinkedBlockingDeque 的帮助下 。

// Java Program Demonstrate offer()
// method of Deque when Null is passed
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>();
  
        if (DQ.offer(10))
            System.out.println("The Deque is not full and 10 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(15))
            System.out.println("The Deque is not full and 15 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(25))
            System.out.println("The Deque is not full and 25 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(20))
            System.out.println("The Deque is not full and 20 is inserted");
        else
            System.out.println("The Deque is full");
  
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}

输出:

The Deque is not full and 10 is inserted
The Deque is not full and 15 is inserted
The Deque is not full and 25 is inserted
The Deque is not full and 20 is inserted
Deque: [10, 15, 25, 20]

下面的程序说明了由这个方法抛出的异常。

程序5: 显示 NullPointerException.

// Java Program Demonstrate offer()
// method of Queue when Null is passed
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
  
public class GFG {
    public static void main(String[] args)
        throws NullPointerException
    {
  
        // create object of Queue
        Deque<Integer> DQ
            = new LinkedBlockingDeque<Integer>();
  
        // Add numbers to end of Deque
        DQ.offer(7855642);
        DQ.offer(35658786);
        DQ.offer(5278367);
  
        // when null is inserted
        DQ.offer(null);
  
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}

输出

Exception in thread "main" java.lang.NullPointerException
    at java.util.concurrent.LinkedBlockingDeque.offerLast(LinkedBlockingDeque.java:357)
    at java.util.concurrent.LinkedBlockingDeque.offer(LinkedBlockingDeque.java:641)
    at GFG.main(GFG.java:21)

注意: 其他两个异常是内部的,取决于编译器,因此不能在代码中显示。

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程