转换Char数组toString()返回质量
我试图反转一个字符串
1st convert to a Char[] 2nd reverse the Char[] 3rd convert CHar[] back to String by toString() method
我检查过toString()的输入是“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
要将char[]
转换为String
,您应该使用String.valueOf(char[])
。
与所有其他数组实现一样, char[].toString()
使用Object.toString()
的默认实现,这是
getClass()。getName()+’@’+ Integer.toHexString(hashCode())
- Java:Long result = -1:无法从int转换为long
- maven / surefire在unit testing后清理的好方法是什么,无论它们是否通过?
- EventListenerList触发顺序
- Java:通过Object(In | Out)putStreams阻塞SocketChannel上是否可以进行并发读写?
- java是否与C#“using”子句等效
- 解释使用Hibernate映射自动递增的复合id序列的行为
- 仅编译Maven中的选定文件
- java中PKCS#5 PBKDF1的算法名称是什么? 什么是java中的CryptoPP :: PKCS5_PBKDF1的替代品?
- 使用Selenium 3.0启动Firefox 46.0.1时获取IllegalStateException