{"id":65975,"date":"2025-05-03T09:11:39","date_gmt":"2025-05-03T01:11:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65975\/"},"modified":"2025-05-03T09:11:39","modified_gmt":"2025-05-03T01:11:39","slug":"java%e4%b8%adchar%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%8a%a0%e5%85%a5%e5%85%83%e7%b4%a0-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65975\/","title":{"rendered":"java\u4e2dchar\u6570\u7ec4\u600e\u4e48\u52a0\u5165\u5143\u7d20"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u5411 char \u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\u7b26 +\uff0c\u521b\u5efa\u65b0\u6570\u7ec4\u5e76\u8fd4\u56de\u3002\u4f7f\u7528 system.arraycopy() \u65b9\u6cd5\uff0c\u590d\u5236\u539f\u6570\u7ec4\u5143\u7d20\u5230\u65b0\u6570\u7ec4\u3002\u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u590d\u5236\u539f\u6570\u7ec4\u5143\u7d20\u5e76\u6dfb\u52a0\u65b0\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111409392081559.jpg\" class=\"aligncenter\" title=\"java\u4e2dchar\u6570\u7ec4\u600e\u4e48\u52a0\u5165\u5143\u7d20\u63d2\u56fe\" alt=\"java\u4e2dchar\u6570\u7ec4\u600e\u4e48\u52a0\u5165\u5143\u7d20\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u5411 char \u6570\u7ec4\u6dfb\u52a0\u5143\u7d20<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0cchar \u6570\u7ec4\u662f\u4e00\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8e\u5b58\u50a8\u5b57\u7b26\u6570\u636e\u3002\u8981\u5411 char \u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u7528\u3002<\/p>\n<p><strong>1. \u4f7f\u7528\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\u7b26 +<\/strong><\/p>\n<ul>\n<li> <strong>\u8bed\u6cd5\uff1a<\/strong>char[] \u6570\u7ec4 = char[] \u6570\u7ec4 + \u5b57\u7b26\u6216\u5b57\u7b26\u4e32<\/li>\n<li>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>char[] arr = {'a', 'b', 'c'};\nchar[] newArr = arr + 'd'; \/\/ \u65b0\u6570\u7ec4 newArr \u5c06\u5305\u542b {'a', 'b', 'c', 'd'}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>2. \u4f7f\u7528 System.arraycopy() \u65b9\u6cd5<\/strong><\/p>\n<ul>\n<li> <strong>\u8bed\u6cd5\uff1a<\/strong>System.arraycopy(\u6e90\u6570\u7ec4, \u6e90\u6570\u7ec4\u8d77\u59cb\u4f4d\u7f6e, \u76ee\u6807\u6570\u7ec4, \u76ee\u6807\u6570\u7ec4\u8d77\u59cb\u4f4d\u7f6e, \u5143\u7d20\u6570\u91cf)<\/li>\n<li>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>char[] arr = {'a', 'b', 'c'};\nchar[] newArr = new char[arr.length + 1];\nSystem.arraycopy(arr, 0, newArr, 0, arr.length);\nnewArr[newArr.length - 1] = 'd'; \/\/ \u6dfb\u52a0\u5143\u7d20 'd' \u5230 newArr \u7684\u672b\u5c3e<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>3. \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\u5e76\u590d\u5236\u5143\u7d20<\/strong><\/p>\n<ul>\n<li> <strong>\u8bed\u6cd5\uff1a<\/strong>char[] newArr = new char[\u539f\u6570\u7ec4.length + 1];<\/li>\n<li>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>char[] arr = {'a', 'b', 'c'};\nchar[] newArr = new char[arr.length + 1];\nfor (int i = 0; i &lt; arr.length; i++) {\nnewArr[i] = arr[i];\n}\nnewArr[newArr.length - 1] = 'd'; \/\/ \u6dfb\u52a0\u5143\u7d20 'd' \u5230 newArr \u7684\u672b\u5c3e<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u4f7f\u7528\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\u7b26 + \u65f6\uff0c\u76ee\u6807 char \u6570\u7ec4\u5c06\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u9700\u8981\u521b\u5efa\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 char \u6570\u7ec4\u3002<\/li>\n<li>System.arraycopy() \u65b9\u6cd5\u4e0d\u4f1a\u6539\u53d8\u6e90\u6570\u7ec4 arr\u3002<\/li>\n<li>\u5728\u4f7f\u7528\u4ee5\u4e0a\u4efb\u4f55\u65b9\u6cd5\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u6709\u8db3\u591f\u7684\u7a7a\u95f4\u6765\u5bb9\u7eb3\u989d\u5916\u7684\u5143\u7d20\u3002\u5426\u5219\uff0c\u53ef\u80fd\u4f1a\u629b\u51fa ArrayIndexOutOfBoundsException \u5f02\u5e38\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2dchar\u6570\u7ec4\u600e\u4e48\u52a0\u5165\u5143\u7d20\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 java \u4e2d\uff0c\u5411 char \u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\u7b26 +\uff0c\u521b\u5efa\u65b0\u6570\u7ec4\u5e76\u8fd4\u56de\u3002\u4f7f\u7528 system.arraycopy() \u65b9\u6cd5\uff0c\u590d\u5236\u539f\u6570\u7ec4\u5143\u7d20\u5230\u65b0\u6570\u7ec4\u3002\u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u590d\u5236\u539f\u6570\u7ec4\u5143\u7d20\u5e76\u6dfb\u52a0\u65b0\u5143\u7d20\u3002 \u5982\u4f55\u5728 Java \u4e2d\u5411 char \u6570\u7ec4\u6dfb\u52a0\u5143\u7d20 \u5728 Java \u4e2d\uff0cchar \u6570\u7ec4\u662f\u4e00\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8e\u5b58\u50a8\u5b57\u7b26\u6570\u636e\u3002\u8981\u5411 char \u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u7528\u3002 1. \u4f7f\u7528\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\u7b26 + \u8bed\u6cd5\uff1achar[] \u6570\u7ec4 = char[] \u6570\u7ec4 + \u5b57\u7b26\u6216\u5b57\u7b26\u4e32 \u793a\u4f8b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b char[] arr = {&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;}; char[] newArr = arr + &#8216;d&#8217;; \/\/ \u65b0\u6570\u7ec4 newArr \u5c06\u5305\u542b {&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;} \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 System.arraycopy() [&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-65975","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65975","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=65975"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65975\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}