Sai A Sai A
Updated date Jul 06, 2023
In this blog, we will learn how to convert an array into a queue in Java. Explore multiple methods, including using built-in classes like LinkedList and ArrayDeque, as well as a manual implementation using ArrayList. Each method is explained in detail, along with sample code and output.

Introduction:

In Java, arrays and queues are two commonly used data structures that serve different purposes. While arrays offer efficient random access to elements, queues provide a convenient way to implement a first-in, first-out (FIFO) data structure. However, there are scenarios where we might need to convert an array into a queue to take advantage of queue-specific operations. In this tutorial, we will explore multiple methods to convert an array into a queue in Java, along with detailed explanations and sample code.

Method 1: Using the LinkedList Class

The LinkedList class in Java provides an implementation of the Queue interface, making it a suitable choice for converting an array to a queue. We can iterate over the array and enqueue each element into the LinkedList.

import java.util.LinkedList;
import java.util.Queue;

public class ArrayToQueueConversion {
    public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5};
        Queue<Integer> queue = new LinkedList<>();

        for (Integer element : array) {
            queue.add(element);
        }

        // Output: [1, 2, 3, 4, 5]
        System.out.println(queue);
    }
}

By using the add() method of the LinkedList class, we can enqueue each element from the array into the queue. The resulting queue can then be printed using the toString() method, which displays the elements in square brackets separated by commas.

Method 2: Using the ArrayDeque Class

Java's ArrayDeque class also implements the Queue interface, providing an alternative approach to convert an array into a queue. We can iterate over the array and use the offer() method to add each element to the ArrayDeque.

import java.util.ArrayDeque;
import java.util.Queue;

public class ArrayToQueueConversion {
    public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5};
        Queue<Integer> queue = new ArrayDeque<>();

        for (Integer element : array) {
            queue.offer(element);
        }

        // Output: [1, 2, 3, 4, 5]
        System.out.println(queue);
    }
}

Similar to the LinkedList approach, we iterate over the array and add each element to the ArrayDeque using the offer() method. The resulting queue is then printed using the toString() method.

Method 3: Manual Implementation using an ArrayList

If you prefer not to use built-in classes, you can create your own queue implementation using an ArrayList and follow the FIFO principle. This method involves removing elements from the front of the array and appending them to the end.

import java.util.ArrayList;
import java.util.List;

public class ArrayToQueueConversion {
    public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5};
        List<Integer> queue = new ArrayList<>();

        for (Integer element : array) {
            queue.add(element);
        }

        for (int i = 0; i < array.length; i++) {
            int frontElement = queue.remove(0);
            queue.add(frontElement);
        }

        // Output: [1, 2, 3, 4, 5]
        System.out.println(queue);
    }
}

In this manual implementation, we use an ArrayList as the underlying data structure. We add each element from the array to the end of the list. Next, we iterate over the array length and remove the element at index 0 (representing thefront of the queue) using the remove() method. We then append the removed element to the end of the list using the add() method. This process effectively shifts the front element to the end, simulating the behavior of a queue.

Conclusion:

In this tutorial, we explored different methods to convert an array into a queue in Java. We covered three approaches: using the LinkedList class, the ArrayDeque class, and a manual implementation using an ArrayList. Each method provides a way to achieve the desired conversion, and the choice depends on specific requirements and preferences.

Comments (0)

There are no comments. Be the first to comment!!!