arrays - Java ArrayQueue enqueue and dequeue throwing arrayQueueException -


i having problem when trying dequeue arrayqueue> variable type arrayqueue , pass parameter method.

so got 2 methods, 1 sort() no parameter , other 1 merge() takes in 2 arrayqueue parameters.

i got global variable called queue:

private arrayqueue<arrayqueue<e>> queue; 

and inside sort(), dequeueing first 2 items queue , pass them merge() parameters:

public void sort() {     while (queue.size() >= 2) {         arrayqueue<e> first = q.dequeue();         arrayqueue<e> second = q.dequeue();         system.out.print("1st" + firstelement);         system.out.print("2nd" + secondelement);          merge(first,second)     } }  public e dequeue() throws arrayqueueexception {     if (isempty() == true)         throw new arrayqueueexception("queue error");     if (front == rear) {         front = front - 1;         rear = rear - 1;     } else         front = front + 1;     size--;     return q[front]; } 

then inside merge:

private arrayqueue<e> merge(arrayqueue<e> q1, arrayqueue<e> q2) throws arrayqueueexception {     system.out.println("q1 " + q1.tostring());     arrayqueue<e> merged = new arrayqueue<e>();     string e1 = (string) q1.dequeue();     system.out.println("e1 " + e1);     string e2 = (string) q2.dequeue();      merged.enqueue(q2.dequeue());             if (q2.isempty()) {                 // add remaining q1 elements                 while (!q1.isempty()) {                     merged.enqueue(q1.dequeue());                 }                 break;             }             // take element q2             e2 = (string) q2.dequeue(); } 

and output console is:

1st[the]2nd[ragged]q1 [the] e1 null exception in thread "main" arrayqueueexception: queue error @ arrayqueue.dequeue(arrayqueue.java:84) @ queuesort.merge(queuesort.java:41) @ queuesort.sort(queuesort.java:73) @ queuesort.main(queuesort.java:122) 

queuesort.java:41 line: merged.enqueue(q2.dequeue()); not sure why won't dequeue.

also, not sure why e1 became null because trying perform dequeue , cast string. need string later part. ideas? in advance.

the main problem appears dequeue function. returning isempty(). check on isempty() , check size right before point. don't see items being enqueued arrayqueue before try dequeue. possible have q1 , q2 set don't put items on returning nothing.

one possible problem variable name queue. queue interface , possible things getting confused in sort before pass dequeue. should change name of queue queue or more distinct. best have variables begin lowercase.

also, style, in dequeue function isempty() not need compared true if return boolean it. like:

if (isempty()) 

the isempty() function resolves true or false , if statement acts on value.

i hope helps.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -