简体   繁体   English

List.valueOf()在List中使用char []的行为<char[]>

[英]String.valueOf() behavior with char[] in List<char[]>

I was working with char[] and Collection with the below code :- 我正在使用char[]Collection使用以下代码: -

    char[] ch = { 'a', 'b', 'c' };
    List<char[]> chList = new ArrayList<char[]>();
    chList.add(new char[]{'d','e','f'});
    chList.add(new char[]{'g','h','i'});
    String chSt = String.valueOf(ch);
    String chListSt = chList.toString(); 
    System.out.println(chSt);  // outputs  abc
    System.out.println(chListSt); // outputs [[C@8288f50b, [C@b6d2b94b] instead [def, ghi]

Now what I observed above is :- 现在我在上面观察到的是: -

    String chSt = String.valueOf(ch);

I know the above code behaviour is correct for char[] in String.valueOf() , so for the above code abc is printed. 我知道上面的代码行为对于String.valueOf() char[]是正确的,所以对于上面的代码,打印出abc Now consider the next line. 现在考虑下一行。

    String chListSt = chList.toString(); 

Also for the above code I know the toString() for List is defined in AbstractList and in the code of this overriden toString() I found buffer.append(next); 同样对于上面的代码,我知道ListtoString()是在AbstractList中定义的,在这个overriden toString()的代码中我找到了buffer.append(next); which calls String.valueOf() method on the char[] which corresponds to next here. 它调用char[]上的String.valueOf()方法,该方法对应于next

So it should also print like [def, ghi] , as in the direct case with char[] in the line String chSt = (String.valueOf(ch)); 因此它也应该像[def, ghi]一样打印,就像在String chSt = (String.valueOf(ch));使用char[]的直接情况一样String chSt = (String.valueOf(ch));

Why is this change in behaviour is there in both the cases while same method String.valueOf() is called on the char[] ? 为什么这两种情况都会出现这种变化,而在char[]上调用相同的方法String.valueOf()

You're seeing the difference of calling different overloads of String.valueOf . 你看到调用String.valueOf不同重载的区别。 Here's a simpler example: 这是一个更简单的例子:

public class Test {
    public static void main(String[] args) {
        char[] chars = { 'a', 'b', 'c' };
        System.out.println(String.valueOf(chars));  // abc
        Object object = chars;
        System.out.println(String.valueOf(object));  // [C@...
    }
}

The call you've found in StringBuffer or StringBuilder is just going to call String.valueOf(Object) - which then calls toString() on the array (after checking the reference isn't null). 您在StringBufferStringBuilder找到的调用只是调用String.valueOf(Object) - 然后在数组上调用toString() (在检查引用后不为null)。 Arrays don't override toString in Java, hence the output you're getting. 数组不会覆盖Java中的toString ,因此您获得的输出。

Code String chListSt = chList.toString(); 代码String chListSt = chList.toString(); simply called toString() implementation of List. 简单地调用List的toString()实现。 List implementation calls toString() implementation on the elements. List实现调用元素上的toString()实现。

Your current list has array objects in the list as an elements, so you are getting an array.toString() representation on the console which results hexadecimal code printed for char[]. 您当前的列表将列表中的数组对象作为元素,因此您将在控制台上获得一个array.toString()表示,从而生成为char []打印的十六进制代码。

Instead of char[] try to save string in the array. 而不是char []尝试在数组中保存字符串。 It will have same performance and results will be readable. 它将具有相同的性能,结果将是可读的。

char[] ch = { 'a', 'b', 'c' };

The above one is a literal and hence valueof() method gives it value. 上面的一个是文字,因此valueof()方法赋予它值。

chList.add(new char[]{'d','e','f'});
chList.add(new char[]{'g','h','i'});

These lists have objects stored in them as a result you are getting their hashcodes... 这些列表中存储了对象,因此您获得了他们的哈希码...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM