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
toString()
[foo, bar, ba
Use toArray() to print the stack values
toArray()
public void printStack(Stack stack) { // Method 1: String values = Arrays.toString(stack.toArray()); System.out.println(values); // Method 2: Object[] vals = stack.toArray(); for (Object obj : vals) { System.out.println(obj); } }