-1

I was having some problem when trying to perform a sort in Java. Here is the reference link that I found: Sorting an array with Stacks and Queues. Basically I am in the same situation as him, just that his is an Integer Queue whereas mine is String ArrayQueue. Here is my sort():

private ArrayQueue<ArrayQueue<E>> Q;
private int n;
public void sort() {
    if (Q.size() <= 1) return;

   ArrayQueue<E> left  = new ArrayQueue<E>(), right = new ArrayQueue<E>();
    while (!Q.isEmpty()) {
        left.enqueue(Q.dequeue());
        if (!Q.isEmpty()) {
           right.enqueue(Q.dequeue());
        }
    }
    merge(left, right);
}

For my dequeue(), it is returning E:

 public E dequeue() throws ArrayQueueException {
         if(f == r){
          f = f -1;
          r = r -1;
         }else
          f = f +1;    
         size --;
         return Q[f];
}

As for the enqueue, it is taking E element as parameter:

 public void enqueue(E element) throws ArrayQueueException {
   //Code
}

The errors are at the left.enqueue and right.enqueue part inside the sort(). Error message as:

The method enqueue(E) in the type ArrayQueue<E> is not applicable for the arguments (ArrayQueue<E>)

I was following the reference link that I found. Any ideas?

Community
  • 1
  • 1
QWERTY
  • 2,303
  • 9
  • 44
  • 85

1 Answers1

1

With

private ArrayQueue<ArrayQueue<E>> Q;

Q.dequeue() will return a ArrayQueue<E>.

With

ArrayQueue<E> left;

left.enqueue(..) expects an E.

An ArrayQueue<E> is not an E.

That's why you get the error

The method enqueue(E) in the type ArrayQueue<E> is not applicable for the arguments (ArrayQueue<E>)

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I see but is there anyway to fix this so that my left and right remains as ArrayQueue cause I need them for the merge(). I figured out a way to remove the error by replacing the type of left and right to ArrayQueue>. However, with these changes, it clash with my merge() – QWERTY Jun 17 '15 at 15:49
  • Any ideas? I am still stuck. Or any way so that my dequeue can return E instead of ArrayQueue? – QWERTY Jun 17 '15 at 16:02
  • @Denise I don't know your merge algorithm, so I can't help with that. You asked about the compilation error, so I provided these details. If you have a question about merge sort with queues and that question you linked to doesn't answer it, ask a new question. – Sotirios Delimanolis Jun 17 '15 at 16:04
  • The merge() actually just taking in 2 ArrayQueue parameters and the return type of this method is ArrayQueue. I actually have not done the algorithm inside it and I just wanted to make the code with no error until the sort() part. – QWERTY Jun 17 '15 at 16:07