Java Deque offerLast()方法
Deque接口 的 offerLast(E e) 方法将指定的元素插入到Deque的末端,如果可以在不违反容量限制的情况下立即这样做。这个方法比add()方法要好,因为这个方法在容器容量已满时不会抛出一个异常,因为它返回false。
语法
boolean offerLast(E e)
参数: 该方法接受一个强制性参数 e ,它是要插入到Deque末端的元素。
返回: 该方法在成功插入时返回真,否则返回假。
异常: 该函数抛出三种异常,描述如下。
- ClassCastException :当要输入的元素的类别阻止它被添加到这个容器中。
- IllegalArgumentException :当元素的某些属性阻止它被添加到Deque中。
- NullPointerException :当要插入的元素被传递为空,而Deque的接口不允许空元素。
下面的程序说明了Deque的offerLast()方法。
程序1: 在 LinkedList 的帮助下 。
// Java Program Demonstrate offerLast()
// 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.offerLast(10))
System.out.println("The Deque is not full and 10 is inserted");
else
System.out.println("The Deque is full");
if (DQ.offerLast(15))
System.out.println("The Deque is not full and 15 is inserted");
else
System.out.println("The Deque is full");
if (DQ.offerLast(25))
System.out.println("The Deque is not full and 25 is inserted");
else
System.out.println("The Deque is full");
if (DQ.offerLast(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 offerLast()
// 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.offerLast(10))
System.out.println("The Deque is not full and 10 is inserted");
else
System.out.println("The Deque is full");
if (DQ.offerLast(15))
System.out.println("The Deque is not full and 15 is inserted");
else
System.out.println("The Deque is full");
if (DQ.offerLast(25))
System.out.println("The Deque is not full and 25 is inserted");
else
System.out.println("The Deque is full");
if (DQ.offerLast(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 offerLast()
// 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.offerLast(10))
System.out.println("The Deque is not full and 10 is inserted");
else
System.out.println("The Deque is full");
if (DQ.offerLast(15))
System.out.println("The Deque is not full and 15 is inserted");
else
System.out.println("The Deque is full");
if (DQ.offerLast(25))
System.out.println("The Deque is not full and 25 is inserted");
else
System.out.println("The Deque is full");
if (DQ.offerLast(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 offerLast()
// 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.offerLast(10))
System.out.println("The Deque is not full and 10 is inserted");
else
System.out.println("The Deque is full");
if (DQ.offerLast(15))
System.out.println("The Deque is not full and 15 is inserted");
else
System.out.println("The Deque is full");
if (DQ.offerLast(25))
System.out.println("The Deque is not full and 25 is inserted");
else
System.out.println("The Deque is full");
if (DQ.offerLast(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 offerLast()
// 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.offerLast(7855642);
DQ.offerLast(35658786);
DQ.offerLast(5278367);
// when null is inserted
DQ.offerLast(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 GFG.main(GFG.java:21)
注意: 其他两个异常是内部的,取决于编译器,因此不能在代码中显示。
参考资料 :https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#offerLast-E-