1

I'm having problems removing an element from the queue. through extensive debugging I found that all the elements are being added, but when I try to remove one, it gives me the same element over and over. here is the code:

private synchronized String accessMyQueue(char[] myInput) {
   String myOutput="";
   myOutput=convertToString(myQueue.remove());
   System.out.println("accessqueue removing:" + myOutput);
}

//and so you can see what's going on in convertToString...

private String convertToString(char[] a) {
   String myString = new String(a);
   return myString.trim();
}
1
  • What type of object is myQueue? It's not declared in the snippet you provided. Commented Aug 5, 2010 at 0:42

1 Answer 1

8

If myQueue is an instance of a standard Java class implementing the Queue interface, the chance that you have found a bug with it are ... well, close enough to zero that we can discount it as a possibility.

If, on the other hand, you've implemented your own queue then, yes, there may well be a problem but, since psychic debugging is not yet a well-established field of endeavour, you're going to have to show us the code for it :-)

I see one of two possibilities. The first is that you are somehow setting each node of your queue to the same value and you may well be removing items okay (you can detect this by adding one item then trying to remove two). This is far more likely in a language like C where you may inadvertently reuse the same pointer but it's far less likely in Java with its improved strings.

The second and most likely is that you're not removing the element from the queue when you call remove, rather you're returning the string without adjusting whatever your underlying data structure is (or, alternatively, adjusting it wrongly).

Short of seeing the code, that's about as good as I can do.


After your update that you were indeed using LinkedList, I thought I'd give it a shot with a very simple example xx.java:

import java.util.LinkedList;
import java.util.Queue;
public class xx {
    public static void main (String args[]) {
        Queue<String> myQueue = new LinkedList<String>();
        myQueue.add ("abc");
        myQueue.add ("def");
        System.out.println (myQueue.size());
        System.out.println (myQueue.remove());
        System.out.println (myQueue.size());
        System.out.println (myQueue.remove());
        System.out.println (myQueue.size());
        try {
            System.out.println (myQueue.remove());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This outputs:

2
abc
1
def
0
java.util.NoSuchElementException
    at java.util.LinkedList.remove(LinkedList.java:805)
    at java.util.LinkedList.removeFirst(LinkedList.java:151)
    at java.util.LinkedList.remove(LinkedList.java:498)
    at xx.main(xx.java:14)

as expected.

So, bottom line is, I think we're going to need to see more of your code. It's difficult to conceive that, if there were a bug in LinkedList or the Queue interface, it wouldn't have been found yet by the millions of other users :-)

You also want to try putting the System.out.println (myQueue.size()); line at a few strategic places in your code to see what's happening with the queue. This may give you an indication as to what's going on.

Sign up to request clarification or add additional context in comments.

6 Comments

@pax - it is a well known fact that Jon Skeet can do psychic debugging on a good day :-)
He already knows the answer to this problem and has fixed it, but he's cleverly withholding the information to maximize the learning opportunity for all the rest of us.
oops...haha...yes, I'm using the normal java Queue...here's the declaration: private Queue<char[]> myQueue = new LinkedList<char[]>();
my understanding of .remove() is that it's supposed to return the string and remove it from the queue...is that not correct?
and if it matters, I'm using java 1.6
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.