I've created stack and queue arrays which hold integers and have a max capacity of ten. I added the values 1-11 and then will remove and print each value until empty. The queue is not only printing 11 values, but also printing 1 instead of 11 for the first value. The stack however is only printing and removing the 10 values but is giving me an IndexOutBoundsException that is not being caught. Any idea why?

Output:

run:Print queue: 11 2 3 4 5 6 7 8 9 10 11 Print stack: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1at lab4.Stack.pop(Stack.java:44)at lab4.Lab4.main(Lab4.java:54)10 9 8 7 6 5 4 3 2 1 BUILD FAILED (total time: 0 seconds)

Main:

 Queue queue=new Queue(10);Stack stack=new Stack(10);queue.insert(1);queue.insert(2);queue.insert(3);queue.insert(4);queue.insert(5);queue.insert(6);queue.insert(7);queue.insert(8);queue.insert(9);queue.insert(10);queue.insert(11);stack.push(1);stack.push(2);stack.push(3);stack.push(4);stack.push(5);stack.push(6);stack.push(7);stack.push(8);stack.push(9);stack.push(10);stack.push(11);System.out.println("Print queue: ");while (!queue.isEmpty()) {long n=queue.remove();System.out.print(n + " ");}System.out.println("");System.out.println("Print stack: ");while (!stack.isEmpty()) {Object value=stack.pop();System.out.print(value + " ");}

Stack.java

public class Stack {private int[] stackArray; // Stack array of objectsprivate int maxSize=10; // Max size of the stackprivate int top; // top integer of stackpublic Stack(int maxSize) {stackArray=new int[maxSize];top=-1;}public boolean isEmpty() {return (top==maxSize); // true if stack is empty}public boolean isFull() {return (top==maxSize - 1); // true if full}public int size() {return (top + 1); // Return the # of items}public void push(int item) // put element into array{if (!isFull()) {top++;stackArray[top]=item;}}public Object pop() {if (!isEmpty()) {top--;return stackArray[top + 1];} elsethrow new IllegalStateException("Stack empty");}public Object peek() {return stackArray[top];}public String toString() {return "Size: " + maxSize + " Capcaity: " + top; // complete: show the size and capacity}

}

Queue.java

public class Queue {private int maxSize=10;private int[] queueArray;private int front;private int rear;private int nItems;// Constructorpublic Queue(int s) {queueArray=new int[maxSize];front=0;rear=-1;nItems=0;}// Insert item at rear of queuepublic void insert(int j) {if (rear==maxSize - 1) { // Deal with wraparoundrear=-1;}queueArray[++rear]=j;nItems++;}public int remove() {int temp=queueArray[front++];if (front==maxSize) {front=0;}nItems--;return temp;}public int peekFront() {return queueArray[front];}public int peekRear() {return queueArray[front];}public boolean isEmpty() {return (nItems==0);}public boolean isFull() {return (nItems==maxSize);}public int size() {return nItems;}

}

  • Try to debug a simple failing example.– MrSmith42Feb 14 at 19:53
  • Just a general FYI, naming your classes after things that already exist in core Java is generally a bad idea (there already exists a Queue interface and Stack class).– azurefrogFeb 14 at 19:53
  • 1-11 is 11 items, your max size is 10.– TylerFeb 14 at 19:55
  • Looks like Stack.isEmpty() is off by one. When it is full, top=9.– Johnny MoppFeb 14 at 20:00
  • Yeah you're right. isEmpty() method should be top==maxSize + 1. However, I'm still getting the out of bounds exception– DevinFeb 14 at 20:04

These are pretty simple fixes! For the queue, you need to set your maxSize to 11. This will allow you to print each number in the queue correctly without printing the 11 in front. Long story short, given you have 11 total elements in your queue, a max queue size of 11 is necessary.

public class Queue {private int maxSize=11;private int[] queueArray;private int front;private int rear;private int nItems;}

Now, in terms of your stack, this was also a pretty simple fix. As of right now, your code is returning top==maxSize (or 10) when the stack is empty. This is causing an issue with the pop function. However, when the stack is empty, top should equal -1. So make sure to set top==-1 in the isEmpty() function. You should also change int maxSize=10 to int maxSize=11 and make the necessary change in the pop() function as well as this is where the error is occurring. Finally, make sure to change the value in your stack initialization from 10 to 11. I've changed your code and it should now work properly! Enjoy!

public class Stack {private int[] stackArray; // Stack array of objectsprivate int maxSize=11; // Max size of the stackprivate int top; // top integer of stackpublic Stack(int maxSize) {stackArray=new int[maxSize];top=-1;}public boolean isEmpty() {return (top==-1); // true if stack is empty}public boolean isFull() {return (top==maxSize); // true if full}public int size() {return (top-1); // Return the # of items}public void push(int item) // put element into array{if (!isFull()) {top++;stackArray[top]=item;}}public Object pop() {if (!isEmpty()) {return stackArray[top--];} elsethrow new IllegalStateException("Stack empty");}public Object peek() {return stackArray[top+1];}public String toString() {return "Size: " + maxSize + " Capcaity: " + top; // complete: show the size and capacity} 

    Your Answer

     
    discard

    By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.