如果有空間,則Deque接口的addFirst(E e)方法會將參數中傳遞的元素插入到Deque的前麵。如果Deque受容量限製並且沒有剩餘空間可插入,則它將返回IllegalStateException。成功插入後,該函數返回true。
用法:
void addFirst(E e)
參數:此方法接受強製性參數e,該參數是要插入到雙端隊列的前麵的元素。
返回:成功插入後,此方法返回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<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-
相關用法
- Java BlockingDeque addFirst()用法及代碼示例
- Java LinkedList addFirst()用法及代碼示例
- Java ArrayDeque addFirst()用法及代碼示例
- Java LinkedBlockingDeque addFirst()用法及代碼示例
- Java ConcurrentLinkedDeque addFirst()用法及代碼示例
- Java Deque offerFirst()用法及代碼示例
- Java Deque addLast()用法及代碼示例
- Java Deque offerLast()用法及代碼示例
- Java Deque offer()用法及代碼示例
- Java Deque iterator()用法及代碼示例
- Java Deque getFirst()用法及代碼示例
- Java Deque element()用法及代碼示例
- Java Deque descendingIterator()用法及代碼示例
- Java Deque contains()用法及代碼示例
- Java Deque add()用法及代碼示例
- Java Deque getLast()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Deque addFirst() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。