Skip to main content
1 of 5
John
  • 241
  • 2
  • 4

Stack class in Java8

I have a exercise where I have to write a Stack class with the push and pop method. The code compiles and works as it should. My question is: Are there better practices to use in my code? Is there something I should avoid doing?

public class Stack {

private final int INCREMENTSIZE = 1024;
Integer position = 0;

int[] list = null;

public Stack(){
    list = new int[INCREMENTSIZE];
}

public void push(Integer i) {
    if(position == list.length) {
        list = Arrays.copyOf(list, list.length + INCREMENTSIZE);
    }
    list[position++] = i;
}

public Integer pop(){
    return list[--position];
}
}
John
  • 241
  • 2
  • 4