Skip to content
Advertisement

How can I avoid java.lang.ArrayIndexOutOfBoundsException when joining a Queue?

I’m working on a simple(?) exercise for my Data Structures class. It works fine right up until I have an element leave the queue then try to add another one on at which point I get the following error:

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 at queueExercise.IntegerQueue.join(IntegerQueue.java:20) at queueExercise.IntegerQueueTest.main(IntegerQueueTest.java:27)

My code is as follows:

Queue Constructor Class:

public class IntegerQueue {
    private int[] queue;
    private int front;
    private int end;
    private int noInQueue;
    private int count;
    private boolean full;

    public IntegerQueue(int max) {
        queue = new int[max];
        front = end = 0;
        full = false;
    }

    public void join(int  newValue) {
        
        if (isFull()==false) {
            queue[end] = newValue;
            count++;
            if (end == queue.length) {
                end = 0;
            }
            else {
                end++;
            }
            
        }else
            System.out.println("Error: Queue Full");

    }

    public int leave() {
        if (isEmpty()==false) {
            noInQueue = queue[front];
            queue[front]=0;
            front++;
            if (front==queue.length) {
                front = 0;
            }
            count--;
        }
        else {
            System.out.println("Error: Queue Empty");
        }
        System.out.println("Leaving: "+noInQueue);
        return noInQueue;
    }

    public boolean isEmpty() {
        if (count == 0){
            return true;
        }
        else 
            return false;
        
    }

    public boolean isFull() {
        if (count >= queue.length) {
            return true;
        }
        else
            return false;
    }
    
    public void printQueue() {
        if (!isEmpty()) {
            System.out.println("Printing Queue");
            int pos = front;
            int i =0;
            while(i<queue.length) {
                System.out.println(queue[pos]);
                pos++;
                i++;
                if (pos >=queue.length) {
                    pos = 0;
                }
            }
        }
    }
}

Test Class


public class IntegerQueueTest {
    static IntegerQueue q = new IntegerQueue(10);

    public static void main(String[] args) {
        int j;
        System.out.println("Creating Queue");
        for (int i = 0; i <10; i++) {
            j = (int)(Math.random()*100);
            if (!q.isFull()) {
                q.join(j);
                System.out.println("Adding: "+j);
            }
        }
        q.printQueue();
        
        q.join(112);
        
        q.leave();
        q.leave();
        q.leave();
        
        q.printQueue();
        
        q.join(112);
        q.join(254);
        
        q.printQueue();
    }
    
    

}

Advertisement

Answer

The problem is in the join method and more precisely in the condition if (end == queue.length). All you have to do is change it to if (end == queue.length - 1).

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement