Answers for "traverse stack in java"

0

how to traverse a stack in java

Just get the Iterator via iterator():

Stack<YourObject> stack = ...

Iterator<YourObject> iter = stack.iterator();

while (iter.hasNext()){
    System.out.println(iter.next());
}
Or alternatively, if you just want to print them all use the enhanced-for loop:

for(YourObject obj : stack)
{
    System.out.println(obj);
}
Posted by: Guest on March-11-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language