how to traverse a stack in java

Solutions on MaxInterview for how to traverse a stack in java by the best coders in the world

showing results for - "how to traverse a stack in java"
Facundo
25 Sep 2016
1Just get the Iterator via iterator():
2
3Stack<YourObject> stack = ...
4
5Iterator<YourObject> iter = stack.iterator();
6
7while (iter.hasNext()){
8    System.out.println(iter.next());
9}
10Or alternatively, if you just want to print them all use the enhanced-for loop:
11
12for(YourObject obj : stack)
13{
14    System.out.println(obj);
15}