142k views
0 votes
Write java code that displays all the objects in a stack in the order in which they were pushed onto it. after all the objects are displayed, the stack should have the same contents as when you started. files to be submitted: taskademo.java arraystack.java stackinterface.java

1 Answer

1 vote
Let's assume you have a stack called "s".
You can very simply print each element out using a for each loop (the iterable (as shown below), or a classic for each loop, if you prefer). This method does not modify the stack in any way.

s.forEach(System.out::println);
User KNDheeraj
by
8.1k points