当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Deque addLast()用法及代码示例


如果有空间,则Deque Interface的addLast(E e)方法会将在参数中传递的元素插入到Deque的末尾。如果Deque受容量限制并且没有剩余空间可插入,则它将返回IllegalStateException。成功插入后,该函数返回true。

用法:

boolean addLast(E e)

参数:此方法接受强制性参数e,该参数是要在双端队列的末尾插入的元素。

返回:成功插入后,此方法返回true。

异常:该函数引发四个异常,如下所述:



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

以下示例程序旨在说明实现Deque接口的各种类对addLast()方法的实现:

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

Exception thrown while inserting an element when the Deque is already full:java.lang.IllegalStateException:Deque full

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

参考: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#addLast-E-

相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Deque addLast() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。