Java中的 Deque offerLast() 方法
Deque 接口的offerLast(E e)方法在不违反容量限制的情况下立即将指定元素插入到 Deque 的末尾。此方法优于add()方法,因为此方法在容器容量已满时不会抛出异常,因为它返回 false。
句法:
boolean offerLast(E e)
参数:此方法接受一个强制参数e ,它是要插入到 Deque 末尾的元素。
返回:此方法在成功插入时返回 true,否则返回 false。
异常:该函数抛出三个异常,描述如下:
- ClassCastException :当要输入的元素的类阻止它被添加到这个容器时。
- IllegalArgumentException :当元素的某些属性阻止将其添加到双端队列时。
- NullPointerException :当要插入的元素作为 null 传递并且 Deque 的接口不允许 null 元素时。
下面的程序说明了 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 DQ
= new LinkedList();
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 DQ
= new ArrayDeque();
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 DQ
= new ConcurrentLinkedDeque();
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 DQ
= new LinkedBlockingDeque();
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 DQ
= new LinkedBlockingDeque();
// 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: Java/util/Deque.html#offerLast-E-