Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Hi, I'm writing a method which checks a queue. This method takes an item as input. It will check the whole queue and see

JAVA

Hi, I'm writing a method which checks a queue. This method takes an item as input. It will check the whole queue and see if the item is already in the queue. If the item is in the queue, this method does nothing and return false. If this item is not in the queue, the method adds this item to the queue and return true.

For example, the queue is 1,2,3,4. If I give 4 to the method, it should do nothing and return false since 4 is already in the queue. If I give 5 to the method. The method should return true and add 5 to the queue.

The problem is my method works fine when the item is already in the queue. But it crashes when the item is not in the queue. Here is my code. Please help me adjust it.

public boolean enqueueNoDuplicate(T item){ int i=0; while(i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions

Question

=+employee to take on the international assignment?

Answered: 1 week ago

Question

=+differences in home- and host-country costs of living?

Answered: 1 week ago