I am implementing an interface in Java like this:
public interface Stack <E>{
public int size();
public boolean isEmpty();
the class that implements it:
public class StackArray<E> implements Stack<E>{
private Object list[];
private int top=-1;
public StackArray(int n){
list=new Object[n];
}
this works quite well, so when I called it I do this:
public static void main(String[] args) {
// TODO code application logic here
StackArray<Students> st=new StackArray<>(4);
so how I can implement that, but using generics, I have tried this:
public class StackArray<E> implements Stack<E>{
private E st[];
private int top=-1;
public StackArray(int n){
st=(E[]) Array.newInstance(null, n);
}
but I got a nullPointerException, is there a way to surpase this?