Array.toString和数组.toString和hashCode详解

在leetcode第一题(链接)中,遇到了一个Array.toString(result)和result.toString的结果不一致的问题,百度之后没有很清晰的认知,只知道结果看似是乱码,但其实是与hashCode或者内存地址有关的或者是有意义的结果。最近在学习java object类的时候学到:所有类直接或间接地继承了Object类,且Object类的方法中toString方法可以被所有类重写,感觉会对理解上述问题有帮助,于是研究了一下object和array的源码,总结如下。

  1. java.lang.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());
    }
  1. java.util.Arrays
    toString():
/**
     * Returns a string representation of the contents of the specified array.
     * The string representation consists of a list of the array's elements,
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
     * separated by the characters <tt>", "</tt> (a comma followed by a
     * space).  Elements are converted to strings as by
     * <tt>String.valueOf(int)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt> is
     * <tt>null</tt>.
     *
     * @param a the array whose string representation to return
     * @return a string representation of <tt>a</tt>
     * @since 1.5
     */
    public static String toString(int[] a) {
        if (a == null)
            return "null";
        int iMax = a.length - 1;
        if (iMax == -1)
            return "[]";

        StringBuilder b = new StringBuilder();
        b.append('[');
        for (int i = 0; ; i++) {
            b.append(a[i]);
            if (i == iMax)
                return b.append(']').toString();
            b.append(", ");
        }
    }

由上述代码可以得知,arr.toString()方法得到的结果是“类名+@+此对象哈希码的无符号十六进制数”,而Arrays.toString(arr)才是将此数组输出的结果。但是,由代码可以看出此方法输出的数组是有格式的,是"[1,2,3]"类型的,如果想要输出无格式的1 2 3,则需要对该方法进行重写。
3. hashCode
具体更多的hashCode的知识可以自行去了解,这里只简单了解下:Object类的hashCode返回对象的内存地址经过处理后的结构,由于每个对象的内存地址都不一样,所以哈希码也不一样。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中提到,Arrays.toString()是一个工具类中的方法,用来将数组转换成String类型输出的。参数可以是不同类型的数组,如long,float,double,int,boolean,byte,object型的数组。使用这个方法可以方便地将数组打印出来。 引用中提到,直接打印int数组或使用int数组toString方法只会打印出数组的地址,而不是数组的内容。为了打印数组中的数据,可以使用Arrays的toString方法。这个方法会将数组转换为String类型,并输出其中的元素。 具体使用方法可以参考以下示例代码: ```java int[] intArray = {1, 4, 6, 7, 8, 9, 10}; System.out.println(Arrays.toString(intArray)); ``` 这段代码会将int数组`intArray`转换为String类型,并输出其中的元素。输出结果将是`[1, 4, 6, 7, 8, 9, 10]`。 引用中提到,在ECMAScript 3中,Object.prototype.toString方法可用于检测变量的类型。当对数组使用这个方法时,返回的结果是`[object Array]`,表示该变量是一个数组。 综上所述,array.ToString()是一个错误的写法,正确的写法应该是Arrays.toString(array)。这个方法可以将数组转换成String类型输出,并输出数组中的元素。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [javaArrays.toString()详细分析(全)](https://blog.csdn.net/weixin_47872288/article/details/116782849)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Java基础——Arrays.toString()方法](https://blog.csdn.net/qq_41074129/article/details/101030969)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [JavaScript中Object.prototype.toString方法的原理](https://download.csdn.net/download/weixin_38577922/14803418)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值