Eclipse tostring方法输出错误
嗨,我正在写一个测试程序来反转字符串。当我使用toString()方法将字符数组转换为字符串时,我得到了错误的输出。当我尝试使用for循环手动打印数组而不将其转换为字符串时,答案是正确的。我编写的代码如下所示:Eclipse tostring方法输出错误,eclipse,string,tostring,Eclipse,String,Tostring,嗨,我正在写一个测试程序来反转字符串。当我使用toString()方法将字符数组转换为字符串时,我得到了错误的输出。当我尝试使用for循环手动打印数组而不将其转换为字符串时,答案是正确的。我编写的代码如下所示: import java.util.*; public class stringManip { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated metho
import java.util.*;
public class stringManip {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "This is a string";
System.out.println("String=" +str);
//reverse(s);
char[] c = str.toCharArray();
int left = 0;
int right = str.length() - 1;
for (int i = 0; i < (str.length())/2; i++)
{
char temp = c[left];
c[left++] = c[right];
c[right--] = temp;
}
System.out.print("Reverse="+c.toString());
}
}
使用toString()方法时是否有什么地方出错?感谢您的帮助。多谢各位 数组不重写
toString()
方法。因此,您看到的是默认的对象.toString()
实现的输出,该实现包含对象的类型([C
表示字符数组),后跟其哈希代码
要从字符数组构造字符串,请使用
new String(c)
new String(c)