Java Deque addLast()方法
Deque接口的 addLast(E e) 方法在有空间的情况下将参数中传递的元素插入到Deque的末端。如果Deque有容量限制,没有空间可以插入,它将返回IllegalStateException。如果插入成功,该函数返回true。
语法
boolean addLast(E e)
参数: 该方法接受一个强制性参数 e ,它是要插入到Deque末端的元素。
返回: 该方法在成功插入时返回true。
异常: 该函数抛出了四个异常,描述如下。
- ClassCastException :当要输入的元素的类别阻止它被添加到这个容器中。
- IllegalStateException :当容器的容量已经满了,并且插入已经完成。
- IllegalArgumentException : 当元素的某些属性阻止它被添加到Deque中。
- NullPointerException :当要插入的元素被传递为空,而Deque的接口不允许空元素。
下面的程序说明了各种实现Deque接口的类对addLast()方法的实现。
程序1: 在 LinkedList 的帮助下 。
// Java Program Demonstrate addLast()
// method of Deque
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of De1ue
Deque<Integer> DQ
= new LinkedList<Integer>();
// Add numbers to end of Deque
DQ.addLast(7855642);
DQ.addLast(35658786);
DQ.addLast(5278367);
DQ.addLast(74381793);
// before removing print Deque
System.out.println("Deque: " + DQ);
}
}
输出。
Deque: [7855642, 35658786, 5278367, 74381793]
程序2: 在 LinkedBlockingDeque 的帮助下 。
// Java Program Demonstrate addLast()
// method of Deque
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of De1ue
Deque<Integer> DQ
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of Deque
DQ.addLast(7855642);
DQ.addLast(35658786);
DQ.addLast(5278367);
DQ.addLast(74381793);
// before removing print Deque
System.out.println("Deque: " + DQ);
}
}
输出。
Deque: [7855642, 35658786, 5278367, 74381793]
程序3: 在 ArrayDeque 的帮助下 。
// Java Program Demonstrate addLast()
// method of Deque
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of De1ue
Deque<Integer> DQ
= new ArrayDeque<Integer>();
// Add numbers to end of Deque
DQ.addLast(7855642);
DQ.addLast(35658786);
DQ.addLast(5278367);
DQ.addLast(74381793);
// before removing print Deque
System.out.println("Deque: " + DQ);
}
}
输出。
Deque: [7855642, 35658786, 5278367, 74381793]
程序4: 在 ConcurrentLinkedDeque 的帮助下 。
// Java Program Demonstrate addLast()
// method of Deque
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of De1ue
Deque<Integer> DQ
= new ConcurrentLinkedDeque<Integer>();
// Add numbers to end of Deque
DQ.addLast(7855642);
DQ.addLast(35658786);
DQ.addLast(5278367);
DQ.addLast(74381793);
// before removing print Deque
System.out.println("Deque: " + DQ);
}
}
输出。
Deque: [7855642, 35658786, 5278367, 74381793]
下面的程序说明了由addLast()方法抛出的异常。
程序5: 显示 NullPointerException。
// Java Program Demonstrate addLast()
// 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>();
// Add numbers to end of DeQue
DQ.addLast(7855642);
DQ.addLast(35658786);
DQ.addLast(5278367);
try {
// when null is inserted
DQ.addLast(null);
}
catch (Exception e) {
System.out.println(
"Exception thrown "
+ "while inserting null: "
+ e);
}
}
}
输出。
Exception thrown while inserting null: java.lang.NullPointerException
程序6: 显示 IllegalStateException .
// Java Program Demonstrate addLast()
// method of Deque when capacity is full
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>(3);
// Add numbers to end of Deque
DQ.addLast(7855642);
DQ.addLast(35658786);
DQ.addLast(5278367);
try {
// when capacity is full
DQ.addLast(10);
}
catch (Exception e) {
System.out.println(
"Exception thrown "
+ "while inserting an element "
+ "when the Deque is already "
+ "full: " + e);
}
}
}
输出。
当Deque已经满了的时候,在插入一个元素时产生了异常:java.lang.IllegalStateException。Deque已满
注意: 其他两个异常是内部的,取决于编译器,因此不能在在线编译器中显示。
参考资料 :https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#addLast-E-
极客教程