how to traverse stack in java

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

showing results for - "how to traverse stack in java"
Alex
09 Jul 2017
1Iterator value = stack.iterator();
2// Displaying the values
3// after iterating through the stack
4System.out.println("The iterator values are: ");
5while (value.hasNext()) {
6	System.out.println(value.next());
7}
Isabell
18 Sep 2017
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}