File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes stack Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "stack" Watch "stack" New topic
Author

stack

weiliu lili
Ranch Hand

Joined: Apr 11, 2002
Posts: 46
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

Joined: Mar 07, 2002
Posts: 110
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: stack
 
Similar Threads
Implementing a Set using Linked List
retrive Vector from HashMap
hashset
Stack
What is the difference ? int / Interger