java 字符数组使用toString变为乱码(其实是地址)。
因为String字符串可以通过toCharArray()得到字符数组,所以很多初学者就会想到对字符数组使用toString()的方法得到字符串:
String name="name";
char[] a=name.toCharArray();
String str=a.toString();
System.out.print(str);
比如这段代码,像我一样的初学者想在这里对字符数组使用toString()得到字符串再print出去。
表面上看,这似乎正确?但把代码运行一下就发现打印的是“[C@179d3b25”,也就是“类名+@+地址”。通过查看方法的源代码,发现toString的代码是这样的:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
这段代码简单来说,就是返回了“这个类名+@+这个变量的地址值”。
这是所有Object类的toString方法 ,而所有的类都是继承于Object类的,所以只要一个类他不去重写他自身的toString 方法的话,调用toString()都会得到"类名+@+地址值"的形式。
所以这里不能直接使用toString方法将字符数组转换为字符串。
那么怎么将字符数组转换为字符串呢?
(1)直接在构造String时转换
String name="name";
char[] a=name.toCharArray();
String str = new String(a);//利用String类的构造函数来转换
System.out.print(str);//此时输出的就是name了
(2)调用String类的提供的方法的valueOf()
String name="name";
char[] a=name.toCharArray();
String str = String.valueOf(a);//调用String类的提供的方法的valueOf()
System.out.print(str);//此时输出的就是name了
(3)用循环遍历
String name="name";
char[] a=name.toCharArray();
String str = "";
for(int i =0;i<a.lenth;i++){
str+=a[i];
}
System.out.print(str);//此时输出的就是name了
这个地方的String其实可以改为BufferString,这样能提高性能,因为字符串是不可变的,每改动一次其实都是生成了一个新的字符串,浪费资源。而BufferString不会。
最后我要说一下,如果你只是想要在println中输出这个字符数组的内容的话,可以写成这样
String name="name";
char[] a=name.toCharArray();
System.out.println(a);//此时输出的就是name了
这是为什么呢?通过debug得知在println中如果你的这个变量是给数组或者是map,list,set等等集合的话,它会把其中每一个元素都调用toString方法 ,即System.out.println("n"+"a"+"m"+"e");
/**
* Prints an array of characters and then terminate the line. This method
* behaves as though it invokes {@link #print(char[])} and
* then {@link #println()}.
*
* @param x an array of chars to print.
*/
public void println(char[] x) {
if (getClass() == PrintStream.class) {
writeln(x);
} else {
synchronized (this) {
print(x);
newLine();
}
}
}
public void print(char s[]) {
write(s);
}
private void write(char[] buf) {
try {
synchronized (this) {
ensureOpen();
textOut.write(buf);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush) {
for (int i = 0; i < buf.length; i++)
if (buf[i] == '\n') {
out.flush();
break;
}
}
}
} catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
} catch (IOException x) {
trouble = true;
}
}