[JAVA]数组转换成字符串

来源:互联网 发布:鲁能泰山 网络直播 编辑:程序博客网 时间:2024/05/16 11:31

所有类都继承自Object类,Object里有一个方法就是toString(),那么所有的类创建的时候,都有一个toString的方法。
Object类中的toString()方法的源代码如下:

    /**     * Returns a string representation of the object. In general, the     * {@code toString} method returns a string that     * "textually represents" this object. The result should     * be a concise but informative representation that is easy for a     * person to read.     * It is recommended that all subclasses override this method.     * <p>     * The {@code toString} method for class {@code Object}     * returns a string consisting of the name of the class of which the     * object is an instance, the at-sign character `{@code @}', and     * the unsigned hexadecimal representation of the hash code of the     * object. In other words, this method returns a string equal to the     * value of:     * <blockquote>     * <pre>     * getClass().getName() + '@' + Integer.toHexString(hashCode())     * </pre></blockquote>     *     * @return  a string representation of the object.     */    public String toString() {        return getClass().getName() + "@" + Integer.toHexString(hashCode());    }

因为数组类中并没有对toString方法的重写(override),仅仅是重载(overload)为类的静态方法(参见java.util.Arrays)。所以,数组直接使用toString()的结果也是[类型@哈希值]。

所以数组转为字符串应写成:Arrays.toString(A)

参考资料:http://www.cnblogs.com/ningvsban/p/3955483.html

0 0