{"id":37653,"date":"2024-11-26T12:46:46","date_gmt":"2024-11-26T04:46:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37653\/"},"modified":"2024-11-26T12:46:46","modified_gmt":"2024-11-26T04:46:46","slug":"java%e6%80%8e%e4%b9%88%e6%8a%8achar%e6%95%b0%e7%bb%84%e6%94%be%e5%85%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37653\/","title":{"rendered":"java\u600e\u4e48\u628achar\u6570\u7ec4\u653e\u5165"},"content":{"rendered":"<blockquote><p>\n  \u5c06 char \u6570\u7ec4\u653e\u5165\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a\u4f7f\u7528 for \u5faa\u73af\u9010\u4e2a\u5143\u7d20\u6dfb\u52a0\uff1b\u4f7f\u7528 apache commons lang \u5de5\u5177\u7c7b\u8f6c\u6362\uff1b\u5728 java 8+ \u4e2d\u4f7f\u7528 arrays.stream() \u548c maptoobj() \u65b9\u6cd5\u8f6c\u6362\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111308271719606.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u628achar\u6570\u7ec4\u653e\u5165\u63d2\u56fe\" alt=\"java\u600e\u4e48\u628achar\u6570\u7ec4\u653e\u5165\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u5c06 char \u6570\u7ec4\u653e\u5165\u6570\u7ec4\u4e2d<\/strong><\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 for \u5faa\u73af<\/strong><\/p>\n<p>\u4f7f\u7528 for \u5faa\u73af\u662f\u5c06 char \u6570\u7ec4\u653e\u5165\u6570\u7ec4\u4e2d\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u6d89\u53ca\u4f7f\u7528\u4e00\u4e2a for \u5faa\u73af\u6765\u904d\u5386 char \u6570\u7ec4\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5143\u7d20\u9010\u4e2a\u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d\u3002<\/p>\n<pre>char[] chars = { 'a', 'b', 'c', 'd', 'e' };\nString[] arr = new String[chars.length];\n\nfor (int i = 0; i &lt; chars.length; i++) {\n    arr[i] = String.valueOf(chars[i]);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 Apache Commons Lang<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>Apache Commons Lang \u63d0\u4f9b\u4e86\u4e00\u4e2a\u6709\u7528\u7684\u5de5\u5177\u7c7b\uff0c\u53ef\u7528\u4e8e\u8f7b\u677e\u5730\u5c06 char \u6570\u7ec4\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f7f\u7528 Apache Commons Lang\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06 char \u6570\u7ec4\u653e\u5165\u6570\u7ec4\u4e2d\uff1a<\/p>\n<pre>char[] chars = { 'a', 'b', 'c', 'd', 'e' };\nString[] arr = ArrayUtils.toObject(chars);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 3\uff1a\u4f7f\u7528 Java 8+<\/strong><\/p>\n<p>\u5728 Java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 Arrays.stream() \u548c mapToObj() \u65b9\u6cd5\u6765\u8f7b\u677e\u5730\u5c06 char \u6570\u7ec4\u653e\u5165\u6570\u7ec4\u4e2d\uff1a<\/p>\n<pre>char[] chars = { 'a', 'b', 'c', 'd', 'e' };\nString[] arr = Arrays.stream(chars)\n                    .mapToObj(c -&gt; String.valueOf(c))\n                    .toArray(String[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u628achar\u6570\u7ec4\u653e\u5165\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06 char \u6570\u7ec4\u653e\u5165\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a\u4f7f\u7528 for \u5faa\u73af\u9010\u4e2a\u5143\u7d20\u6dfb\u52a0\uff1b\u4f7f\u7528 apache commons lang \u5de5\u5177\u7c7b\u8f6c\u6362\uff1b\u5728 java 8+ \u4e2d\u4f7f\u7528 arrays.stream() \u548c maptoobj() \u65b9\u6cd5\u8f6c\u6362\u3002 \u5982\u4f55\u4f7f\u7528 Java \u5c06 char \u6570\u7ec4\u653e\u5165\u6570\u7ec4\u4e2d \u65b9\u6cd5 1\uff1a\u4f7f\u7528 for \u5faa\u73af \u4f7f\u7528 for \u5faa\u73af\u662f\u5c06 char \u6570\u7ec4\u653e\u5165\u6570\u7ec4\u4e2d\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u6d89\u53ca\u4f7f\u7528\u4e00\u4e2a for \u5faa\u73af\u6765\u904d\u5386 char \u6570\u7ec4\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5143\u7d20\u9010\u4e2a\u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d\u3002 char[] chars = { &#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;, &#8216;e&#8217; }; String[] arr = new String[chars.length]; for (int i = 0; i [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-37653","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=37653"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37653\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}