Printing the stack values in Java

后端 未结 8 1632
小蘑菇
小蘑菇 2021-01-11 12:40

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

8条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-11 13:27

    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.

提交回复
热议问题