-1

I am trying to create a method so that I can be able to insert a node at position n in my custom designed stack (built on an Array). When I use stack.pop() as a parameter in stack.push() I get ArrayIndexOutOfBoundsException: -1.

I have tried to replace stack.pop(stack.push()) with a variable representing it, and I got the same exception (ArrayIndexOutOfBoundsException: -1).

Stack class

public class Stack {

    public Node[] stackList = new Node[12]; 
    int index = 0; //Sets index to 0

    public void push(Node node){ //Adds nodes to the top of the stack.
        stackList[index] = node; 
        if (index < (stackList.length - 1)){ 
            index++;
        }
    }

    public Node pop(){ //Removes node from stack.
        Node output = stackList[index];
        stackList[index] = null;
        index--;
        return output;
    }

    public void printStack(){
        for (int j = 0; j < stackList.length; j++){ 
            stackList[j].printValue();  
        }
    }
    public int size(){
        return stackList.length;
    }

    public void insertNode(int val, int pos, Stack stack){
        Stack tmpstack = new Stack();
        Node value = new Node();
        value.changeValue((val));

        int i=0; //Sets index to 0
        while(i<pos){
            tmpstack.push(stack.pop());
            i++;
        }
        stack.push(value); 
        while(tmpstack.size() > 0) 
            stack.push(tmpstack.pop()); 
        }

Method in main-class

public class Main {
    public static void main(String[] args){
        //Stack
        System.out.println("Starting to print the value of the stack:");
        Stack s = new Stack();
        for (int i = 0; i < 12; i++) {
            Node node = new Node();
            node.changeValue((i+1));
            s.push(node);
        }
        s.printStack();
        s.insertNode(77,5, s); //value, position, stack
        s.printStack();
sveggen
  • 1
  • 2
  • 3
    Can you share your code for Stack.push() and Stack.pop()? – Jordan Sep 23 '19 at 16:39
  • 1
    Since you appear to be using a custom Stack object, only assumptions can be made without you providing the relevant push/pop code. More than likely the return type of pop isn't matching the argument type for push, or the other way around. Also, you reference an issue with `stack.pop(stack.push())` but this is **not** in the code you provided. – h0r53 Sep 23 '19 at 16:45
  • Since you are adding a value your stack size will increase by 1. Are you sure that you push function is able to handle that? – Yoshikage Kira Sep 23 '19 at 16:48
  • 1
    Read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ for tips on debugging your code. – Code-Apprentice Sep 23 '19 at 16:50
  • Please add it in the post instead of here. Also see [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Yoshikage Kira Sep 23 '19 at 16:54
  • 1
    It certainly does (that's why it compiles). The fact that you're trying to pop off an empty stack obviously is the problem, as identified by the fact that adding a variable to hold the result changes nothing. – chrylis -cautiouslyoptimistic- Sep 23 '19 at 17:17
  • How are `stackList` and `index` initialized? It might be useful to just include the entire code for your `Stack` class. – Jordan Sep 23 '19 at 18:51
  • @Jordan, I added the entire code for the `Stack` class now. – sveggen Sep 24 '19 at 18:43

1 Answers1

0

Most likely the problem is here in your Stack class:

public Node[] stackList = new Node[12];

You have a hard limit of 12 on the number of items you can put in your stack.

Your main program adds 12 items, prints the stack, and then tries to insert an item. In your push method, you have this:

public void push(Node node){ //Adds nodes to the top of the stack.
    stackList[index] = node; 
    if (index < (stackList.length - 1)){ 
        index++;
    }
}

After you've added 12 items to the stack, index will be equal to 12. So the first line of code, stackList[index] = node;, tries to update stackList[12]. That's outside the bounds of the array (the valid indexes are 0 through 11).

I'll bet that if you change your main loop to only add 11 things to the stack, and then do the insert, the program will work.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351