Thursday, April 5, 2012

Iterator on java Stack appears wrong

Consider the following:

Stack<Integer> stack = new Stack<Integer>();
 for (Integer item: stack){

I would have expected the output to be 3, 2, 1 , based on the LIFO property of the stack, however surprisingly the output is 1, 2, 3 !!

This is definitely not an expected behavior. However this is something that Sun cannot fix without breaking backward compatibility either:

No comments:

Post a Comment