public class QueueAsArray implements Queue
{
    protected int[] myArray;
    int count, size, rear = 0;

    public QueueAsArray(int newSize)
    {
        myArray = new int[newSize];
        size = newSize;
        count = 0;
    }

    public void purge()
    {
        int index = 0;

        while(count > 0)
        {
            myArray[index] = 0;
            index++;
            count--;
        }
        rear = 0;
    }

    public int getHead() throws ContainerEmptyException
    {
        if(count == 0)
            throw new ContainerEmptyException();
        else
            return myArray[0];
    }

    public void enqueue(int value) throws ContainerFullException
    {
        if(count == size)
            throw new ContainerFullException();
        else
        {
            myArray[rear++] = value;
            count++;
        }   
    }

    public int dequeue() throws ContainerEmptyException
    {
        if(count == 0)
            throw new ContainerEmptyException();
        else
        {
            int value = myArray[0];
            count--;

            for(int x = 0; x <= count; x++)
            {
                myArray[x-1] = myArray[x];
            }

            rear--;
            return value;
        }
    }

    public String toString()
    {
        String report = "Data in the queue is: ";

            for(int x = 0; x < myArray.length; x++)
            {
                report += myArray[x] + " ";
            }

            return report;
    }
}

Was working on an assigment and ran into a problem where my dequeue function isn't working properly. I can't seem to figure out what the problem is. Thought I could get some fresh eyes on this? Anyone know?

**Data in queue before dequeuing (application output):

Data in the queue is: 77 32 56 22 87 1 46 999 131 91 

**Data in queue after 2 consecutive dequeues (application output):

Data in the queue is: 32 56 22 87 1 46 999 131 91 91 
Data in the queue is: 56 22 87 1 46 999 131 91 91 91

Side note: Also, my purge method now displays that last two 91's in the queue even after purging...

Data in the queue is: 0 0 0 0 0 0 0 0 91 91

Thanks in advance guys!

Recommended Answers

All 4 Replies

Do you have code for testing? Post a small program that compiles, executes and shows the problems.

How have you tried debugging the code? I don't see any println statements to print out the values of the variables as they are changed and used to control the codes execution. try adding some so you can see what the computer sees while it execute the code.

public class QueueDriver 
{
    public static void main(String[] args) throws ContainerEmptyException, ContainerEmptyException, ContainerFullException
    {
        System.out.println("#################################");
        System.out.println("##Queue As Array Implementation##");
        System.out.println("#################################\n");

        QueueAsArray myQueue = new QueueAsArray(10);

        myQueue.enqueue(77);
        myQueue.enqueue(32);
        myQueue.enqueue(56);
        myQueue.enqueue(22);
        myQueue.enqueue(87);
        myQueue.enqueue(1);
        myQueue.enqueue(46);
        myQueue.enqueue(999);
        myQueue.enqueue(131);
        myQueue.enqueue(91);
        System.out.println(myQueue.toString());

        myQueue.dequeue();
        System.out.println(myQueue.toString());

        myQueue.dequeue();
        System.out.println(myQueue.toString());

        System.out.println("Head of queue: " + myQueue.getHead());

        myQueue.purge();
        System.out.println(myQueue.toString());
    }
}

Apologies, here is my driver file. My above post has the output.

Have you tried debugging the code by adding printlns to show the values of the variables as they are changed and used?

Can you add some comments to the codes posted output that shows what it should have been? You don't really say what is wrong with the output.

The code you posted does NOT compile. It has many errors: missing class definitions for example.

for(int x = 0; x <= count; x++) {
    myArray[x-1] = myArray[x];
}

If you really executed that code the very first array index will evaluate to -1 and it will throw an index out of bounds exception. So what's really going on?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.