如果可以在不违反容量限制的情况下立即执行此操作,则Deque Interface的offerLast(E e)方法会将指定的元素插入Deque的末尾。此方法优于add()方法,因为在容器的容量已满时,此方法不会引发异常,因为它会返回false。
用法:
boolean offerLast(E e)
参数:此方法接受强制性参数e,该参数是要在双端队列的末尾插入的元素。
返回:成功插入时此方法返回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<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-
相关用法
- Java ArrayDeque offerLast()用法及代码示例
- Java LinkedBlockingDeque offerLast()用法及代码示例
- Java ConcurrentLinkedDeque offerLast()用法及代码示例
- Java BlockingDeque offerLast()用法及代码示例
- Java Deque offerFirst()用法及代码示例
- Java Deque addLast()用法及代码示例
- Java Deque addFirst()用法及代码示例
- Java Deque offer()用法及代码示例
- Java Deque iterator()用法及代码示例
- Java Deque getFirst()用法及代码示例
- Java Deque element()用法及代码示例
- Java Deque descendingIterator()用法及代码示例
- Java Deque contains()用法及代码示例
- Java Deque add()用法及代码示例
- Java Deque getLast()用法及代码示例
- Java java.sql.Date转java.util.Date用法及代码示例
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
- Java Java.lang.string.replace()用法及代码示例
- Java Java.util.ArrayList.add()用法及代码示例
- Java Java.util.ArrayList.addall()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Deque offerLast() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。