📜  Java中的双端队列 addLast() 方法

📅  最后修改于: 2022-05-13 01:55:29.312000             🧑  作者: Mango

Java中的双端队列 addLast() 方法

Deque 接口的 addLast (E e)方法,如果有空格,则将参数中传入的元素插入到 Deque 的末尾。如果 Deque 容量受限且没有空间可插入,则返回IllegalStateException 。该函数在成功插入时返回 true。

句法:

boolean addLast(E e)

参数:此方法接受一个强制参数e ,它是要插入到 Deque 末尾的元素。

返回:此方法在成功插入时返回 true。

异常:该函数抛出四个异常,描述如下:

  • ClassCastException :当要输入的元素的类阻止它被添加到这个容器时。
  • IllegalStateException :当容器的容量已满并且插入完成时。
  • IllegalArgumentException :当元素的某些属性阻止将其添加到双端队列时。
  • NullPointerException :当要插入的元素作为 null 传递并且 Deque 的接口不允许 null 元素时。

下面的程序说明了 addLast() 方法的各种实现 Deque 接口的类的实现

程序 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 DQ
            = new LinkedList();
  
        // 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 DQ
            = new LinkedBlockingDeque();
  
        // 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 DQ
            = new ArrayDeque();
  
        // 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 DQ
            = new ConcurrentLinkedDeque();
  
        // 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 DQ
            = new LinkedBlockingDeque();
  
        // 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 DQ
            = new LinkedBlockingDeque(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);
        }
    }
}
输出:

注意:其他两个异常是内部异常,由编译器引起,因此无法在在线编译器中显示。

参考: https: Java/util/Deque.html#addLast-E-