Why does the toString method in java not seem to work for an array

妖精的绣舞 提交于 2019-11-25 23:38:09

问题


I want to convert a character array to a string object using the toString() method in java. Here is a snippet of the test code I used:

import java.util.Arrays;
class toString{
    public static void main(String[] args){
        char[] Array = {\'a\', \'b\', \'c\', \'d\', \'e\', \'f\'};
        System.out.println(Array.toString());
        }
}

In principle, it should print abcdef, but it is printing random gibberish of the likes of [C@6e1408 or [C@e53108 each time the program executes. I don\'t need an alternative out of this but want to know why this is happening.


回答1:


To get a human-readable toString(), you must use Arrays.toString(), like this:

System.out.println(Arrays.toString(Array));

Java's toString() for an array is to print [, followed by a character representing the type of the array's elements (in your case C for char), followed by @ then the "identity hash code" of the array (think of it like you would a "memory address").

This sad state of affairs is generally considered as a "mistake" with java.

See this answer for a list of other "mistakes".




回答2:


I don't know where you get the idea that "in principle" it should print "abcdef". Where is that documented?

Something like [C@6e1408 is certainly not random gibberish - it's the same way of constructing a string from an object as any other type that doesn't override toString() inherits - it's a representation of the type ([ indicating an array; C indicating the char primitive type) followed by the identity hash code in hex. See the documentation for Object.toString() for details. As it happens, arrays don't override toString.

If you want [a, b, c, d, e, f] you can use Arrays.toString(char[]). If you want abcdef you can use new String(char[]).




回答3:


Just use the following commands to get your abcdef array printed

    String a= new String(Array);

    System.out.println(a);

there you have it problem solved !! now regarding why is printing the other stuff i think those guys above put some useful links for that. Ok gotta go !!




回答4:


Because a char array is an array of primitives and toString() will give you it's default (which is a hash of the object). Some classes will implement toString() to do cooler things, but primitaves will not.




回答5:


Arrays don't override toString. There's a static method: java.util.Arrays.toString that should solve your problem.

import java.util.Arrays;
class toString {
    public static void main(String[] args){
        char[] Array = {'a', 'b', 'c', 'd', 'e', 'f'};
        System.out.println(Arrays.toString(Array));
    }
}



回答6:


The default implementation of the toString method of the char [] class returns a String representation of the base address of the array, which is what is being printed here. We cannot change it, since the class of char [] is not extendable.




回答7:


char[] Array = { 'a', 'b', 'c', 'd', 'e', 'f' };
System.out.println(Array);

It should print abcdef.




回答8:


There is a spelling mistake of "Array.toString()" to "Arrays.toString(Array)" I guess so, and instead of writing name.toString(), pass the name as an argument and Write as above.




回答9:


this way I found worked:

public String convertToString(char[] array, int length)
{
String char_string;
String return_string="";
int i;

   for(i=0;i<length;i++)
   {
       char_string=Character.toString(array[i]);
       return_string=return_string.concat(char_string);
   }
   return return_string;
}


来源:https://stackoverflow.com/questions/7060016/why-does-the-tostring-method-in-java-not-seem-to-work-for-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!