Printing the stack values in Java

后端 未结 8 1614
小蘑菇
小蘑菇 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:24

    Another method is the join method of String which is similar to the join method of Python:

    "" + String.join("/", stack)
    

    This will return the string containing all the elements of the stack and we can also add some delimiter by passing in the first argument.

    Example: Stack has two elements [home, abc]

    Then this will return "/home/abc".

提交回复
热议问题