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

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

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

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

句法:

boolean add(E e)

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

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

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

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

下面的程序说明了 Deque 的 add() 方法:

程序 1:LinkedList的帮助下。

Java
// Java Program Demonstrate add()
// 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.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}


Java
// Java Program Demonstrate add()
// 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.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}


Java
// Java Program Demonstrate add()
// 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.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}


Java
// Java Program Demonstrate add()
// 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.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}


Java
/// Java Program Demonstrate add()
// 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.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        // when null is inserted
        DQ.add(null);
 
        // before removing print DeQue
        System.out.println("DeQue: " + DQ);
    }
}


Java
// Java Program Demonstrate add()
// 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.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
 
        // when capacity is full
        DQ.add(10);
 
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}


输出:
Deque: [7855642, 35658786, 5278367, 74381793]




程序 2:ArrayDeque的帮助下。

Java

// Java Program Demonstrate add()
// 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.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}
输出:
Deque: [7855642, 35658786, 5278367, 74381793]




程序 3:ConcurrentLinkedDeque的帮助下。

Java

// Java Program Demonstrate add()
// 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.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}
输出:
Deque: [7855642, 35658786, 5278367, 74381793]




程序 4:LinkedBlockingDeque的帮助下。

Java

// Java Program Demonstrate add()
// 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.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}
输出:
Deque: [7855642, 35658786, 5278367, 74381793]




下面的程序说明了 add() 方法抛出的异常

程序 5:显示NullPointerException

Java

/// Java Program Demonstrate add()
// 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.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        // when null is inserted
        DQ.add(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 java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:334)
    at java.util.concurrent.LinkedBlockingDeque.add(LinkedBlockingDeque.java:633)
    at GFG.main(GFG.java:20)

程序 6:显示IllegalStateException

Java

// Java Program Demonstrate add()
// 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.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
 
        // when capacity is full
        DQ.add(10);
 
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}

输出:

Exception in thread "main" java.lang.IllegalStateException: Deque full
    at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:335)
    at java.util.concurrent.LinkedBlockingDeque.add(LinkedBlockingDeque.java:633)
    at GFG.main(GFG.java:21)

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

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