Thursday, April 5, 2012

Iterator on java Stack appears wrong

Consider the following:

Stack<Integer> stack = new Stack<Integer>();
 stack.push(1);stack.push(2);stack.push(3);
 
 for (Integer item: stack){
  System.out.println(item);
 }

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:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4475301



No comments:

Post a Comment