📜  Java中的deque addFirst() 方法和示例

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

Java中的deque addFirst() 方法和示例

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

句法:

void addFirst(E e)

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

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

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

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

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

程序 1:LinkedList的帮助下。

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

程序 2:ArrayDeque的帮助下。

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

程序 3:ConcurrentLinkedDeque的帮助下。

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

程序 4:LinkedBlockingDeque的帮助下。

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

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

程序 5:显示NullPointerException

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

输出:

Exception in thread "main" java.lang.NullPointerException
    at java.util.concurrent.LinkedBlockingDeque.offerFirst(LinkedBlockingDeque.java:342)
    at java.util.concurrent.LinkedBlockingDeque.addFirst(LinkedBlockingDeque.java:325)
    at GFG.main(GFG.java:20)

程序 6:显示IllegalStateException

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

输出:

Exception in thread "main" java.lang.IllegalStateException: Deque full
    at java.util.concurrent.LinkedBlockingDeque.addFirst(LinkedBlockingDeque.java:326)
    at GFG.main(GFG.java:21)

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

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