Java Collection Tutorial - Java ArrayDeque.addFirst(E e)








Syntax

ArrayDeque.addFirst(E e) has the following syntax.

public void addFirst(E e)

Example

In the following code shows how to use ArrayDeque.addFirst(E e) method.

// w w  w.  j a  v a 2s  .c om


import java.util.ArrayDeque;
import java.util.Deque;

public class Main {
   public static void main(String[] args) {
      
      
      Deque<Integer>  deque = new ArrayDeque<Integer> (5);

      
      deque.add(25);
      deque.add(2);
      deque.add(35);

      deque.addFirst(10);
      deque.addFirst(3);
      deque.addFirst(1);//now, element 1 will be at the front

      deque.add(45);
      deque.add(40);

      System.out.println(deque);
   }
}

The code above generates the following result.