Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the Collections Framework, there is a branch related to Queues. A queue is a data structure that is ordered and whose elements are worked

In the Collections Framework, there is a branch related to Queues. A queue is a data structure that is ordered and whose elements are worked with from only the beginning and end of the data structure. An element is ordered when it holds a specific position in the data structure. Queues adhere to the “first in, first out”, or FIFO, method of processing elements. When adding to a queue, the element is added to the end of the data structure and when removing from the queue, the element that is at the beginning of the data structure is removed.

Common queue operations:

  • offer: add to the end of the data structure
  • poll: remove from the front of the data structure
  • peek: get the first element in the data structure

In this activity you will learn about how to create and use queues. Please follow the steps below:

Steps:

  1. Add the following code inside of the main() method:
 

ArrayDeque queue = new ArrayDeque<>();

The statement we added creates an ArrayDeque, a subtype of Queue, and assigns it to a corresponding reference variable. This queue can store Integer objects.

  1. Now that we have a queue to work with, let's add some elements to it. Add the following code inside of the main() method, below any code you have in it so far:
 

queue.offer(1);

queue.offer(2);

queue.offer(3);

queue.offer(4);

queue.offer(5);

The offer() method adds to the queue. Elements are added to the end of the queue.

  1. Next, let's see what the first element in the queue is. Add the following code inside of the main() method, below any code you have in it so far:
 

System.out.println(queue.peek());

The peek() method returns the value of the first element in the queue. The output to the console should be 1.

  1. Now it's your turn. Create a queue that will hold String objects.
  2. Add the following String objects into your queue: "Zero", "One", "Two", "Three", "Four", "Five"
  3. Create a for-each loop that iterates over your queue.
  4. For every iteration of the loop, remove and print out the current first element using the System.out.println() and poll() method.
  5. Run the program. Notice how the queue polls the first item that went into the queue first. This is called FIFO (First in first out).

Test:

Use the test provided.

Sample output:

 

1

Zero

One

Two

Three

Four

Five

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Here are the steps to handle this 1 Create a queue that can hold String objects java ArrayDeque stri... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Vector Mechanics for Engineers Statics and Dynamics

Authors: Ferdinand Beer, E. Russell Johnston, Jr., Elliot Eisenberg, William Clausen, David Mazurek, Phillip Cornwell

8th Edition

73212229, 978-0073212227

More Books

Students also viewed these Mechanical Engineering questions

Question

describe the key elements of work;

Answered: 1 week ago