import java.util.Iterator;
import java.util.Stack;
public class Main {
public static void main(String[] argv) {
Stack<String> lStack = new Stack<String>();
lStack.add("ONE");
lStack.add("TWO");
lStack.add("THREE");
Iterator<String> lIterator = lStack.iterator();
while ( lIterator.hasNext() ) {
System.out.println( lIterator.next());
}
}
}
import java.util.Stack;
public class Main {
public static void main(String[] argv) {
Stack<String> lStack = new Stack<String>();
lStack.add("ONE");
lStack.add("TWO");
lStack.add("THREE");
Iterator<String> lIterator = lStack.iterator();
while ( lIterator.hasNext() ) {
System.out.println( lIterator.next());
}
}
}
I was hoping that the output from the above code would be :
THREE
TWO
ONE
But, the output was :
ONE
TWO
THREE
The reason why i expect it to be like the first output shown above is - It is a Stack and a Stack orders elements in a Last In First Out (LIFO) manner. Hence the Iterator returned from the call lStack.Iterator() should preserve this ordering. Am I missing something obvious ?
No comments:
Post a Comment