Java printing stack values

In Java, I want to print the contents of a stack. The toString() method outputs them in square brackets, separated by commas: [foo, bar, baz] .

The question is, how do I get rid of them and print only the variables?

My code is:

 Stack myStack = new Stack (); for(int j=0;j<arrayForVar.length;j++) { if(arrayForVar[j][1]!=null) { System.out.printf("%s \n",arrayForVar[j][1]+"\n"); myStack.push(arrayForVar[j][1]); } System.out.printf("%s \n",myStack.toString()); 

This answer worked for me:

Use the toString method on the stack and use the replaceAll method to replace all instances of square brackets with blankstring. Like this:

 System.out.print( myStack.toString().replaceAll("\\[", "").replaceAll("]", "")); 
+7
source share
7 answers

There is a workaround.

You can convert it to an array and then print using Arrays.toString(Object[]) :

System.out.println(Arrays.toString(myStack.toArray()));

+10
source

Use the same loop you used to fill the stack and print individual elements to your liking. You cannot change the behavior of toString unless you send a route to a Stack subclass that I would not recommend. If the Stack source code is under your control, then just fix the toString implementation there.

+2
source

Use toArray() to print stack values

 public void printStack(Stack<Integer> 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); } } 
+1
source

From the documentation of the toString () method AbstractCollection . Thus, you cannot do this if you do not define your own Stack or implement the custom parameter toString (), iterating over Stack

public String toString ()

Returns a string representation of this collection. The string representation consists of a list of elements in the collection in the order in which they are returned by its iterator enclosed in square brackets ("[]"). Adjacent elements are separated by the characters "," (comma and space). Elements are converted to strings, just like String.valueOf (Object).

This implementation creates an empty string buffer, adds a left bracket and iterates through the collection, adding a string representation of each element in turn. After adding each item except the last, the string "," is added. Finally, the right bracket is added. The string is obtained from the string buffer and returned.

0
source

Drop the offer into the pool here. This may or may not be possible depending on your stack implementation.

This is an anonymous inner class for overriding toString () in this particular case. This is one of the local implementation methods of the subclass that Marco mentions. Your Stack instance will look something like this:

 Stack s = new Stack(){ public String toString(){ // query the elements of the stack, build a string and return nicelyFormattedString; } }; ... 
0
source
 stack.forEach(System.out::println); 

Using Java 8 and later stream functions

0
source

Try this:

  System.out.println(Arrays.asList(stackobject.toArray())); System.out.println(Arrays.toString(stackobject.toArray())); 
-2
source

Source: https://habr.com/ru/post/923963/


All Articles