Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

stack

 
weiliu lili
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
public class Test {

public static void main(String[] args){
Integer a = new Integer(8);
Integer b = new Integer(4);
Integer c = new Integer(4);
Stack vec=new Stack();

Iterator itr;
vec.add(a);
vec.add(b);
vec.add(c);
itr = vec.iterator();
while (itr.hasNext()) {
System.out.println("" + itr.next());
}
}
}
the output is 8,4,4, why not 4,4,8 according to "last in ,first out"
thanks
 
Stephane Weber
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
It's because for having this behavior of LastInFirstOut, you have to use the specific interface of the class Stack (here, the method pop would have returned you the expected result of 4,4,8).
But as you see, the Stack class extends Vector. And therefore, the method ierator() that you called returned you an iterator as as it would have done for a simple Vector (keeping the order in which you entered the data).
Hope this helps

Stephane
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic