Answers for "iterate stack in java"

1

how to traverse stack in java

Iterator value = stack.iterator();
// Displaying the values
// after iterating through the stack
System.out.println("The iterator values are: ");
while (value.hasNext()) {
	System.out.println(value.next());
}
Posted by: Guest on September-06-2021
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