【发布时间】:2015-02-18 20:28:38
【问题描述】:
这是我的代码:
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());
}
}
控制台打印出这个:
[hi, bye, awful]
我以为用toString会去掉[],为什么还在呢?
编辑: 如果 toString 不是摆脱[]的正确方法,那么正确的方法是什么?
【问题讨论】:
-
你为什么认为
toString会去掉括号? -
这是默认实现。
-
It is hard coded in the source code。正确的方法?创建一个打印内容的自己的方法。
-
不要使用 toString() 依赖特定的输出格式
-
我会在 dup Q/A 中选择 this answer。