I have a exercise where I have to write a Stack class with the push and pop methods. The code compiles and works as it should. 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];
}
}
final
, someone could extend it and make the subclass do whatever they want. Or there's always reflection.) But I do agree that renaming a perfectly ordinary LinkedList into something else is pointless. (For that matter, using LinkedList for anything is usually a poor choice anyway — at least 99% of the time ArrayList is just as good or better.) \$\endgroup\$