I am new at java and I am trying to write an array Stack now I want to avoid loitering in pop () function,
public class Stack {
private int[] s;
private int N=0;
public Stack(int capacity)
{
s= new int [capacity];
}
public boolean IsEmpty ()
{
return N==0;
}
public void push (int x)
{
s[N++]=x;
}
public int pop ()
{
int x=s[--N];
s[N]=null;
return x;
}
when it decrements that value in, there is still pointer to the element that has been took off the stack now I tried to set the removed item to null but the compiler gives me exception
what can I do in order to delete the pointer of the removed item?!
int), not with objectsN.int, tonull. Only descendants ofObjectcan benull. It should suffice to only decrement your counter (N) inpop(), and be sure to correctly set the last stack element inpush().