这个 报价(E) 方法 Deque接口 如果可以在不违反容量限制的情况下立即将指定元素插入此数据块中。这种方法比其他方法更可取 添加() 方法,因为该方法在容器的容量已满时不会引发异常,因为它返回false。
null
语法:
boolean offer(E e)
参数: 此方法接受一个强制参数 E 这是要插入到Deque中的元素。
返回: 此方法在成功插入时返回true,否则返回false。
例外情况: 该函数引发四个异常,如下所述:
- 类别例外 :当要输入的元素的类阻止它添加到此容器时。
- 非法数据异常 :当元素的某些属性阻止将其添加到Deque时。
- 空指针异常 :当要插入的元素作为null传递,并且Deque的接口不允许null元素时。
下面的程序演示了Deque的offer()方法:
项目1: 借助于 链表 .
// 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: 借助于 ConcurrentLinkedEque .
// 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: 展示 空指针异常 .
// 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-
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END