The code below should pop 4 elements, but it just shows 2 elements. I am not able to understand the prolem there:
Stack stack = new Stack();
stack.push("Obj1");
stack.push("Obj2");
stack.push("Obj3");
stack.push("Obj4");
System.out.println(stack.size());
for(int i=0;i<stack.size();i++)
{
System.out.println("Stack header is: " + stack.peek());
System.out.println("Popped value is: " + stack.pop());
}
Output:
Stack header is: Obj4
Popped value is: Obj4
Stack header is: Obj3
Popped value is: Obj3