0

Here is my code:

public class StackStudy{
    public static void main(String[] args){
        Stack<String> st = new Stack<String>();
        st.push("hi");
        st.push("bye");
        st.push("awful");
        System.out.println(st.toString());
    }
}

and console prints out this:

[hi, bye, awful]

I thought using toString will get rid of the [], why is it still there?

EDIT: If toString is not the proper way to get rid of[], what is the right way to do it?

OPK
  • 4,120
  • 6
  • 36
  • 66
  • 14
    Why did you think `toString` would get rid of the brackets? – Alexis King Dec 19 '14 at 21:09
  • 2
    It's the default implementation. – Alexis C. Dec 19 '14 at 21:09
  • 1
    [It is hard coded in the source code](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/AbstractCollection.java#AbstractCollection.toString%28%29). A proper way? Create an own method that prints the content. – Tom Dec 19 '14 at 21:12
  • Remove it using `String#substring`. It's not that hard if you try it. – Luiggi Mendoza Dec 19 '14 at 21:13
  • Do you want a programmatic way to do it, i.e. iteration, or do you want the brackets to be removed? – Compass Dec 19 '14 at 21:13
  • @AlexisKing because I want to print out a string and so far I have only learned toString. what is the right way to get rid of the []? – OPK Dec 19 '14 at 21:14
  • 2
    Don't use toString() to rely on a specific output format – Falmarri Dec 19 '14 at 21:14
  • If you don't like the `toString()` implementation in `Stack`, you can always make your own subclass which has its own implementation. – azurefrog Dec 19 '14 at 21:14
  • 1
    I would opt for [this answer](http://stackoverflow.com/a/22577565/1065197) in the dup Q/A. – Luiggi Mendoza Dec 19 '14 at 21:16

3 Answers3

2

Because the toString() method of Vector (or possibly AbstractCollection) includes the brackets (and Stack inherits that toString()). You could remove them by taking the substring like

String str = st.toString();
System.out.println(str.substring(1, str.length() - 1));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

overriding toString() gives you full control over what get's returned. Therefor, you can skip the brackets. However, you've only called the toString() method (which is actually what the System.out.println() is actually doing as well. Therefor, the outcome is as expected.

Adrian B.
  • 1,592
  • 1
  • 20
  • 38
0

The following code would do the trick.

public class StackStudy{
    public static void main(String[] args){
        Stack<String> st = new Stack<String>();
        st.push("hi");
        st.push("bye");
        st.push("awful");
        String stackAsString = st.toString();
        System.out.println(stackAsString.substring(1, stackAsString.length() - 1));
    }
 }
kuriouscoder
  • 5,394
  • 7
  • 26
  • 40
  • tried but not working, not sure I understood this part: String stackAsString = st.toString(); System.out.println(stackAsString.substring(1, stackAsString.length() - 1)); – OPK Dec 19 '14 at 21:25