0

I'm studying stacks. I was trying to write my code for pop() when I encountered that Java doesn't allow to "convert from null to int"...

How can I remove an element from array? Even if I typecast null to integer, it throws a null pointer exception.

Most other tutorials I've seen online just change the top of stack and do nothing about the value at the index.

Check the int pop() function below

static class Stack{

        int[] holder;
        int capacity;
        int top;

        //Constructor
        Stack(int a){
            holder = new int[a];
            capacity = a-1;
            top = -1;
        }

        //Method to Print Stack
        void PrintStack(){
            System.out.println(Arrays.toString(this.holder));
        }

        //Method to PUSH
        void push(int a){
            //Check for StackOverflow
            if(top == capacity){
                System.out.println("Stack Overflow!!!");
                }
            else{
                top++;
                this.holder[top] = a;
            }
        }

        //Method to POP
        int pop(){
            //Check for StackUnderflow
            if(top == -1){
                System.out.println("Stack Underflow");
            }
            else{
                return this.holder[top];
                this.holder[top] = null;
                top--;
            }
        }
    }
1
  • Don't set the value to null just leave it. As long as your top is correct you should not have a problem. You can't set primitive type int to null. Commented Oct 9, 2014 at 18:52

3 Answers 3

1

Since this is an array of (primitive) integers, you cannot assign null to an element in it. In fact, when the array is created, its elements are not null, they are set to zero which is the default initial value for an integer.

If the array was an array of objects, it would have been important to replace popped elements with null, otherwise the objects would not be garbage-collected. That problem does not exist when the array is of a primitive type, so you can just leave the value as is.

However, I would advise you to change your array-printing method, because whatever the current size of the stack, it always prints the entire array. That would be confusing. You should print the array only up to the "top" element.

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

Comments

1

If you want to represent null in an array of integers, use the java Integer type, not the native int type.

It's not terrible to leave the old value in unused spots in your stack. It used to be that there was no distinction between the number zero and a null value, so there was no advantage to modifying values in unused slots.

Comments

1

I think the following might be more an accurate way of representing what you're trying to accomplish. Convert int to an Integer and return null which is now allowed because it's an Integer type.

public class Stack{

        Integer[] holder;
        int capacity;
        int top;

        //Constructor
        Stack(int a){
            holder = new Integer[a];
            capacity = a-1;
            top = -1;
        }

        //Method to Print Stack
        void PrintStack(){
            System.out.println(Arrays.toString(this.holder));
        }

        //Method to PUSH
        void push(int a){
            //Check for StackOverflow
            if(top == capacity){
                System.out.println("Stack Overflow!!!");
                }
            else{
                top++;
                this.holder[top] = a;
            }
        }

        //Method to POP
        Integer pop(){
            //Check for StackUnderflow
            if(top == -1){
                return null;
            }
            else{
                return this.holder[top--];
            }
        }
    }

2 Comments

One question, the line return this.holder[top--]; will return the top value? I'm confused, because seems like it will return the value of the second last entered integer instead of the last entered...
It might be beneficial to write a unit test to prove or disprove the behavior. Using top-- takes the current item in the array (returns that) and then decrements the index of top. This is opposed to --top.

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.