{"id":37957,"date":"2024-11-26T10:40:22","date_gmt":"2024-11-26T02:40:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37957\/"},"modified":"2024-11-26T10:40:22","modified_gmt":"2024-11-26T02:40:22","slug":"java%e5%8d%95%e5%bc%95%e5%8f%b7%e6%80%8e%e4%b9%88%e5%ad%98%e5%ad%97%e7%ac%a6%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37957\/","title":{"rendered":"Java\u5355\u5f15\u53f7\u600e\u4e48\u5b58\u5b57\u7b26\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u5355\u5f15\u53f7\u7528\u4e8e\u5b58\u50a8\u5355\u4e2a\u5b57\u7b26\uff0c\u8981\u5b58\u50a8\u5b57\u7b26\u6570\u7ec4\uff0c\u9700\u9075\u5faa 4 \u4e2a\u6b65\u9aa4\uff1a1. \u58f0\u660e\u6570\u7ec4\uff1b2. \u4f7f\u7528\u5355\u5f15\u53f7\u5206\u914d\u5b57\u7b26\uff1b3. \u521d\u59cb\u5316\u6570\u7ec4\uff1b4. \u4f7f\u7528 character.tochars() \u65b9\u6cd5\u3002\u5b57\u7b26\u6570\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u7d22\u5f15\u4ece 0 \u5f00\u59cb\uff0c\u6bcf\u4e2a\u5b57\u7b26\u5206\u914d\u4e00\u4e2a\u7d22\u5f15\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111313512280473.jpg\" class=\"aligncenter\" title=\"Java\u5355\u5f15\u53f7\u600e\u4e48\u5b58\u5b57\u7b26\u6570\u7ec4\u63d2\u56fe\" alt=\"Java\u5355\u5f15\u53f7\u600e\u4e48\u5b58\u5b57\u7b26\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u5355\u5f15\u53f7\u5982\u4f55\u5b58\u50a8\u5b57\u7b26\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u5355\u5f15\u53f7 (&#8216;) \u7528\u6765\u5b58\u50a8\u5355\u4e2a\u5b57\u7b26\u3002\u8981\u5b58\u50a8\u5b57\u7b26\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u58f0\u660e\u6570\u7ec4<\/strong><\/p>\n<pre>char[] myArray = new char[size];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d size \u662f\u6240\u9700\u6570\u7ec4\u7684\u5927\u5c0f\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>2. \u4f7f\u7528\u5355\u5f15\u53f7\u5206\u914d\u5b57\u7b26<\/strong><\/p>\n<pre>myArray[index] = 'c';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d index \u662f\u6570\u7ec4\u7d22\u5f15\uff0c&#8217;c&#8217; \u662f\u8981\u5206\u914d\u7684\u5b57\u7b26\u3002<\/p>\n<p><strong>3. \u521d\u59cb\u5316\u6570\u7ec4<\/strong><\/p>\n<pre>char[] myArray = { 'a', 'b', 'c' };<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5b57\u7b26 &#8216;a&#8217;\u3001&#8217;b&#8217; \u548c &#8216;c&#8217; \u7684\u6570\u7ec4\u3002<\/p>\n<p><strong>4. \u4f7f\u7528 Character.toChars() \u65b9\u6cd5<\/strong><\/p>\n<p>Character.toChars() \u65b9\u6cd5\u5c06\u4e00\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\u3002<\/p>\n<pre>char[] myArray = Character.toChars(myChar);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u6bcf\u4e2a\u5b57\u7b26\u90fd\u5c06\u5206\u914d\u4e00\u4e2a\u7d22\u5f15\u3002<\/li>\n<li>\u6570\u7ec4\u7d22\u5f15\u4ece 0 \u5f00\u59cb\u3002<\/li>\n<li>\u53ef\u4ee5\u4f7f\u7528 length \u5c5e\u6027\u83b7\u53d6\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/li>\n<li>\u5b57\u7b26\u6570\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4e00\u65e6\u521b\u5efa\u5c31\u65e0\u6cd5\u4fee\u6539\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u4e3a 5 \u7684\u5b57\u7b26\u6570\u7ec4\nchar[] myArray = new char[5];\n\n\/\/ \u4f7f\u7528\u5355\u5f15\u53f7\u5206\u914d\u5b57\u7b26\nmyArray[0] = 'H';\nmyArray[1] = 'e';\nmyArray[2] = 'l';\nmyArray[3] = 'l';\nmyArray[4] = 'o';\n\n\/\/ \u6253\u5370\u6570\u7ec4\u5185\u5bb9\nfor (char c : myArray) {\n    System.out.print(c);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>Hello<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJava\u5355\u5f15\u53f7\u600e\u4e48\u5b58\u5b57\u7b26\u6570\u7ec4\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>\u5728 java \u4e2d\uff0c\u5355\u5f15\u53f7\u7528\u4e8e\u5b58\u50a8\u5355\u4e2a\u5b57\u7b26\uff0c\u8981\u5b58\u50a8\u5b57\u7b26\u6570\u7ec4\uff0c\u9700\u9075\u5faa 4 \u4e2a\u6b65\u9aa4\uff1a1. \u58f0\u660e\u6570\u7ec4\uff1b2. \u4f7f\u7528\u5355\u5f15\u53f7\u5206\u914d\u5b57\u7b26\uff1b3. \u521d\u59cb\u5316\u6570\u7ec4\uff1b4. \u4f7f\u7528 character.tochars() \u65b9\u6cd5\u3002\u5b57\u7b26\u6570\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u7d22\u5f15\u4ece 0 \u5f00\u59cb\uff0c\u6bcf\u4e2a\u5b57\u7b26\u5206\u914d\u4e00\u4e2a\u7d22\u5f15\u3002 Java \u5355\u5f15\u53f7\u5982\u4f55\u5b58\u50a8\u5b57\u7b26\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u5355\u5f15\u53f7 (&#8216;) \u7528\u6765\u5b58\u50a8\u5355\u4e2a\u5b57\u7b26\u3002\u8981\u5b58\u50a8\u5b57\u7b26\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u58f0\u660e\u6570\u7ec4 char[] myArray = new char[size]; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d size \u662f\u6240\u9700\u6570\u7ec4\u7684\u5927\u5c0f\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 2. \u4f7f\u7528\u5355\u5f15\u53f7\u5206\u914d\u5b57\u7b26 myArray[index] = &#8216;c&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d index \u662f\u6570\u7ec4\u7d22\u5f15\uff0c&#8217;c&#8217; \u662f\u8981\u5206\u914d\u7684\u5b57\u7b26\u3002 3. \u521d\u59cb\u5316\u6570\u7ec4 char[] myArray = { &#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217; }; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5b57\u7b26 &#8216;a&#8217;\u3001&#8217;b&#8217; [&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-37957","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37957","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=37957"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37957\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}