Convert Char Array toString () возвращаемая масса
Я пытаюсь поменять строку
1st convert to a Char[]
2nd reverse the Char[]
3rd convert CHar[] back to String by toString() method
мы проверили ввод toString () is "ABCD \ 0" но выход "[C @ 2e686cea» ...
У меня есть два вопроса:
1. why toString() output this
2. what's the best way to process String? convert to char[] or using StringBuffer?
Вот'мой код:
public class TestBench_1_2 {
public static String reverseCStr (String str) {
if (str == null) return str;
char[] cArray = str.toCharArray();
int len = str.length();
char temp = '\0';
for (int i = 0; i < (Math.floor((len-1)/2)); ++i) {
temp = cArray[i];
cArray[i] = cArray[len-2-i];
cArray[len-2-i] = temp;
}
// String result = new String(cArray);
String result = cArray.toString();
return result;
}
public static void main (String[] args) {
String[] testStr = {"abcd\0", "abcde\0", "123\0", "ab1p4\0", null};
System.out.printf("Test Strings: \t\t Reverse Result: \n");
String temp = null;
for (String str : testStr) {
System.out.printf("%s \t\t\t %s\n", str, reverseCStr(str));
}
}
}
И здесь'с выводом:
Test Strings: Reverse Result:
abcd [C@2e686cea
abcde [C@8e43b44
123 [C@3feef1eb
ab1p4 [C@604c9c17
null null