Java ArrayDeque add() Method



Description

The Java ArrayDeque add(E e) method inserts the specified element E at the end of the deque. This method is equivalent to addLast(E). Insertion order is maintained while adding the element to the ArrayDeque and we can check the same by printing each element one by one.

Declaration

Following is the declaration for java.util.ArrayDeque.add() method

public boolean add(E e)

Parameters

e − The element to be added in the deque.

Return Value

This method returns true if given element is added successfully into the deque, otherwise it returns false.

Exception

NullPointerException − if the specified element is null.

Example 1

The following example shows the usage of Java ArrayDeque add(E) method to add Integers. We're adding couple of Integers to the ArrayDeque object using add() method calls per element and then print each element to show the elements added.

package com.tutorialspoint;
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque with an initial capacity
      Deque<Integer> deque = new ArrayDeque<Integer>(5);

      // use add() method to add elements in the deque
      deque.add(20);
      deque.add(30);
      deque.add(20);
      deque.add(30);
      deque.add(15);
      deque.add(22);
      deque.add(11);

      // let us print all the elements available in deque
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Number = 20
Number = 30
Number = 20
Number = 30
Number = 15
Number = 22
Number = 11

Example 2

The following example shows the usage of Java ArrayDeque add(E) method to add Strings. We're adding couple of strings to the ArrayDeque object using add() method calls per element and then printing the ArrayDeque using its toString() method.

package com.tutorialspoint;
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque
      Deque<String> deque = new ArrayDeque<String>();

      // use add() method to add elements in the deque
      deque.add("Welcome");
      deque.add("To");
      deque.add("Tutorialspoint");
      System.out.println("ArrayDeque = " + deque);      
   }
}

Output

Let us compile and run the above program, this will produce the following result −

ArrayDeque = [Welcome, To, Tutorialspoint]

Example 3

The following example shows the usage of Java ArrayDeque add(E) method to add Student objects. We're adding couple of Student objects to the ArrayDeque object using add() method calls per element and then printing the ArrayDeque using its toString() method.

package com.tutorialspoint;
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeDemo {
   public static void main(String[] args) {

      // create an empty array deque
      Deque<Student> deque = new ArrayDeque<Student>();

      // use add() method to add elements in the deque
      deque.add(new Student(1, "Julie"));
      deque.add(new Student(2, "Robert"));
      deque.add(new Student(3, "Adam"));
      System.out.println("ArrayDeque = " + deque);      
   }
}

class Student {
   int rollNo;
   String name;
   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

ArrayDeque = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_arraydeque.htm
Advertisements