Create DeQue in Java - How to add Elements to DeQue

Published April 04, 2022


An array Deque can easily be created in Java. You only need to import the java.util.ArrayDeque package. After import, create the array Deque as follows:

ArrayDeque<Type> name_of_array_Deque = new ArrayDeque<>();

 

You can create either a string or an integer ArrayDeque. For instance

// A String type 

ArrayDeque<String> students = new ArrayDeque<>();

// Integer type 

ArrayDeque<Integer> marks = new ArrayDeque<>();

 

 

Example of an ArrayDeque Program

For this example, we will create an ArrayDeque and add three students by using the add(), addFirst(), and addLast() methods.

 

import java.util.ArrayDeque;

public class ArrayDequeClass {

    public static void main(String[] args) {

        ArrayDeque<String> students= new ArrayDeque<>();

        // Add first student using add()

        students.add("Rajkmaar");

        // Add second student using addFirst()

        students.addFirst("Rohit");

        // Add third student using addLast()

        students.addLast("Thakur");

        System.out.println("ArrayDeque: " + students);

    }

}

 

Output:

ArrayDeque: [Rohit, Rajkmaar, Thakur]


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

38 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions