Java Deque addFirst()方法及示例

Java Deque addFirst()方法及示例

Deque接口的 addFirst(E e) 方法在有空间的情况下将参数中传递的元素插入到Deque的前面。如果Deque有容量限制,没有空间可以插入,它将返回IllegalStateException。如果插入成功,该函数返回true。

语法

void addFirst(E e)

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

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

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

  • ClassCastException :当要输入的元素的类别阻止它被添加到这个容器中。
  • IllegalStateException :当容器的容量已满,并且插入已经完成。
  • IllegalArgumentException : 当元素的某些属性阻止它被添加到Deque中。
  • NullPointerException :当要插入的元素被传递为空,而Deque的接口不允许空元素。

下面的程序说明了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<Integer> DQ
            = new LinkedList<Integer>();
  
        // 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<Integer> DQ
            = new ArrayDeque<Integer>();
  
        // 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<Integer> DQ
            = new ConcurrentLinkedDeque<Integer>();
  
        // 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<Integer> DQ
            = new LinkedBlockingDeque<Integer>();
  
        // 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<Integer> DQ
            = new LinkedBlockingDeque<Integer>();
  
        // 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<Integer> DQ
            = new LinkedBlockingDeque<Integer>(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://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#addFirst-E-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程