In Java, I want to print the contents of a Stack. The toString()
method prints them encased in square brackets delimited by commas: [foo, bar, ba
Use the same kind of loop that you used to fill the stack and print individual elements to your liking. There is no way to change the behavior of toString
, except if you go the route of subclassing Stack
, which I wouldn't recommend. If the source code of Stack
is under your control, then just fix the implementation of toString
there.