Java 中的队列 add() 方法
Queue 接口 的 add(E e) 方法,在队列末尾插入传递参数的元素,如果还有空间。如果队列有容量限制且没有剩余的空间可插入,它会返回一个 IllegalStateException 异常。函数在成功插入后返回 true。
语法:
boolean add(E e)
参数: 此方法接受一个必需参数 e ,它是要插入到队列末尾的元素。
返回值: 此方法在成功插入后返回 true 。 异常: 此函数抛出四个异常,如下所述:
- ClassCastException :当要插入的元素的类防止其被添加到该容器中时。
- IllegalStateException :当容器的容量已满并执行插入时。
- IllegalArgumentException :当要插入的元素的某些属性防止其被添加到队列中时。
- NullPointerException :当要插入的元素传递为 null,且队列接口不允许 null 元素时。
以下程序说明了队列的 add() 方法:
程序1: 使用 LinkedList 。
// Java Program Demonstrate add()
// method of Queue
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new LinkedList<Integer>();
// Add numbers to end of Queue
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
Q.add(74381793);
// before removing print queue
System.out.println("Queue: " + Q);
}
}
输出:
Queue: [7855642, 35658786, 5278367, 74381793]
程序2: 使用 ArrayDeque 。
// Java Program Demonstrate add()
// method of Queue
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new ArrayDeque<Integer>();
// Add numbers to end of Queue
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
Q.add(74381793);
// before removing print queue
System.out.println("Queue: " + Q);
}
}
输出:
Queue: [7855642, 35658786, 5278367, 74381793]
程序3: 使用 LinkedBlockingDeque 。
// Java Program Demonstrate add()
// method of Queue
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of Queue
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
Q.add(74381793);
// before removing print queue
System.out.println("Queue: " + Q);
}
}
输出:
队列:[7855642、35658786、5278367、74381793]
程序 4: 使用 ConcurrentLinkedDeque 进行操作。
// Java 程序演示 add()
// Queue 方法
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// 创建 Queue 对象
Queue<Integer> Q
= new ConcurrentLinkedDeque<Integer>();
// 将数字添加到队列尾部
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
Q.add(74381793);
// 在移除前输出队列
System.out.println("队列:" + Q);
}
}
输出:
队列:[7855642, 35658786, 5278367, 74381793]
下面的程序说明了 此方法抛出的异常 : 程序 5: 展示 NullPointerException 。
// Java 程序演示 add()
// Queue 当传入 Null 时的方法
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// 创建 Queue 对象
Queue<Integer> Q
= new LinkedBlockingQueue<Integer>();
// 将数字添加到队列尾部
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
try {
// 当插入 Null 时
Q.add(null);
}
catch (Exception e) {
System.out.println("异常:" + e);
}
}
}
输出:
异常:java.lang.NullPointerException
程序 6: 展示 IllegalStateException 。
// Java 程序演示 add()
// Queue 在容量已满时的方法
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// 创建 Queue 对象
Queue<Integer> Q
= new LinkedBlockingQueue<Integer>(3);
// 将数字添加到队列尾部
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
try {
// 当容量已满时
Q.add(10);
}
catch (Exception e) {
System.out.println("异常:" + e);
}
}
}
输出:
异常:java.lang.IllegalStateException: Queue full
时间复杂度: O(1)
注意: 另外两个异常是内部异常,由编译器引起,因此无法在编译器中显示。
参考资料: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#add-E-
极客教程