{"id":65359,"date":"2025-05-03T16:52:28","date_gmt":"2025-05-03T08:52:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65359\/"},"modified":"2025-05-03T16:52:28","modified_gmt":"2025-05-03T08:52:28","slug":"java%e6%80%8e%e4%b9%88%e5%bb%ba%e5%ad%97%e7%ac%a6%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65359\/","title":{"rendered":"java\u600e\u4e48\u5efa\u5b57\u7b26\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u521b\u5efa\u5b57\u7b26\u6570\u7ec4\uff1a\u58f0\u660e\u6570\u7ec4\uff1achar[] myarray;\u521d\u59cb\u5316\u6570\u7ec4\uff08\u4f7f\u7528 new \u8fd0\u7b97\u7b26\uff09\uff1amyarray = new char[size];\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff1amyarray[index] = &#8216;a&#8217; \u6216 char c = myarray[index]\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111221514853381.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5efa\u5b57\u7b26\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5efa\u5b57\u7b26\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u7528 Java \u521b\u5efa\u5b57\u7b26\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 char[] \u58f0\u660e\u548c\u521d\u59cb\u5316\u5b57\u7b26\u6570\u7ec4\u3002\u4ee5\u4e0b\u4e3a\u521b\u5efa\u5b57\u7b26\u6570\u7ec4\u7684\u6b65\u9aa4\uff1a<\/p>\n<p><strong>\u6b65\u9aa4 1\uff1a\u58f0\u660e\u6570\u7ec4<\/strong><\/p>\n<pre>char[] myArray;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b65\u9aa4 2\uff1a\u521d\u59cb\u5316\u6570\u7ec4\uff08\u4f7f\u7528 new \u8fd0\u7b97\u7b26\uff09<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>myArray = new char[size];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d size \u6307\u5b9a\u4e86\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4e5f\u5c31\u662f\u53ef\u4ee5\u5b58\u50a8\u7684\u5b57\u7b26\u6570\u91cf\u3002<\/p>\n<p><strong>\u6b65\u9aa4 3\uff1a\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\u8bbf\u95ee\u548c\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5b57\u7b26\u3002\u7d22\u5f15\u4ece 0 \u5f00\u59cb\uff0c\u76f4\u5230 size-1\u3002<\/p>\n<pre>myArray[index] = 'a';  \/\/ \u8d4b\u503c\u5b57\u7b26 'a' \u7ed9\u7d22\u5f15\u5904\u7684\u5143\u7d20\nchar c = myArray[index];  \/\/ \u83b7\u53d6\u7d22\u5f15\u5904\u7684\u5b57\u7b26<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>char[] myArray = new char[5];\n\n\/\/ \u521d\u59cb\u5316\u6570\u7ec4\u5143\u7d20\nmyArray[0] = 'H';\nmyArray[1] = 'e';\nmyArray[2] = 'l';\nmyArray[3] = 'l';\nmyArray[4] = 'o';\n\n\/\/ \u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\nSystem.out.println(myArray[0]);  \/\/ \u8f93\u51fa H<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u65b9\u5f0f\uff1a<\/strong><\/p>\n<p>\u9664\u4e86\u4f7f\u7528 new \u8fd0\u7b97\u7b26\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u5f0f\u521b\u5efa\u5b57\u7b26\u6570\u7ec4\uff1a<\/p>\n<ul>\n<li> <strong>\u5b57\u9762\u91cf\uff1a<\/strong>\u76f4\u63a5\u6307\u5b9a\u5b57\u7b26\u5e8f\u5217\uff0c\u4f8b\u5982 char[] myArray = {&#8216;H&#8217;, &#8216;e&#8217;, &#8216;l&#8217;, &#8216;l&#8217;, &#8216;o&#8217;};\u3002<\/li>\n<li> <strong>String.toCharArray()\uff1a<\/strong>\u5c06 String \u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\uff0c\u4f8b\u5982 char[] myArray = &#8220;Hello&#8221;.toCharArray();\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5efa\u5b57\u7b26\u6570\u7ec4\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\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u521b\u5efa\u5b57\u7b26\u6570\u7ec4\uff1a\u58f0\u660e\u6570\u7ec4\uff1achar[] myarray;\u521d\u59cb\u5316\u6570\u7ec4\uff08\u4f7f\u7528 new \u8fd0\u7b97\u7b26\uff09\uff1amyarray = new char[size];\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff1amyarray[index] = &#8216;a&#8217; \u6216 char c = myarray[index]\u3002 \u5982\u4f55\u7528 Java \u521b\u5efa\u5b57\u7b26\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 char[] \u58f0\u660e\u548c\u521d\u59cb\u5316\u5b57\u7b26\u6570\u7ec4\u3002\u4ee5\u4e0b\u4e3a\u521b\u5efa\u5b57\u7b26\u6570\u7ec4\u7684\u6b65\u9aa4\uff1a \u6b65\u9aa4 1\uff1a\u58f0\u660e\u6570\u7ec4 char[] myArray; \u767b\u5f55\u540e\u590d\u5236 \u6b65\u9aa4 2\uff1a\u521d\u59cb\u5316\u6570\u7ec4\uff08\u4f7f\u7528 new \u8fd0\u7b97\u7b26\uff09 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b myArray = new char[size]; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d size \u6307\u5b9a\u4e86\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4e5f\u5c31\u662f\u53ef\u4ee5\u5b58\u50a8\u7684\u5b57\u7b26\u6570\u91cf\u3002 \u6b65\u9aa4 3\uff1a\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20 \u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\u8bbf\u95ee\u548c\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5b57\u7b26\u3002\u7d22\u5f15\u4ece 0 \u5f00\u59cb\uff0c\u76f4\u5230 size-1\u3002 myArray[index] = &#8216;a&#8217;; \/\/ \u8d4b\u503c\u5b57\u7b26 &#8216;a&#8217; \u7ed9\u7d22\u5f15\u5904\u7684\u5143\u7d20 [&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-65359","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65359","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=65359"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65359\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}