{"id":65888,"date":"2025-05-03T17:15:20","date_gmt":"2025-05-03T09:15:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65888\/"},"modified":"2025-05-03T17:15:20","modified_gmt":"2025-05-03T09:15:20","slug":"%e6%80%8e%e4%b9%88%e5%88%9d%e5%a7%8b%e5%8c%96string%e6%95%b0%e7%bb%84-java-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65888\/","title":{"rendered":"\u600e\u4e48\u521d\u59cb\u5316string\u6570\u7ec4 java"},"content":{"rendered":"<blockquote><p>\n  \u5b57\u7b26\u4e32\u6570\u7ec4\u5728 java \u4e2d\u6709\u591a\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\uff1a\u76f4\u63a5\u521d\u59cb\u5316\uff1astring[] names = {&#8220;john&#8221;, &#8220;jane&#8221;, &#8220;tom&#8221;};\u4f7f\u7528 new \u5173\u952e\u5b57\uff1astring[] names = new string[3];\u4f7f\u7528 arrays.fill() \u65b9\u6cd5\uff1astring[] names = new string[3]; arrays.fill(names, &#8220;unknown&#8221;);\u4f7f\u7528\u6d41\u521d\u59cb\u5316\uff1astring[] names = stream.of(&#8220;john&#8221;, &#8220;jane&#8221;, &#8220;tom&#8221;).\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111314273742243.jpg\" class=\"aligncenter\" title=\"\u600e\u4e48\u521d\u59cb\u5316string\u6570\u7ec4 java\u63d2\u56fe\" alt=\"\u600e\u4e48\u521d\u59cb\u5316string\u6570\u7ec4 java\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u521d\u59cb\u5316\uff1f<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u5b57\u7b26\u4e32\u6570\u7ec4\u53ef\u4ee5\u6709\u591a\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\uff1a<\/p>\n<p><strong>1. \u76f4\u63a5\u521d\u59cb\u5316<\/strong><\/p>\n<pre>String[] names = {\"John\", \"Jane\", \"Tom\"};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 new \u5173\u952e\u5b57<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[] names = new String[3];\nnames[0] = \"John\";\nnames[1] = \"Jane\";\nnames[2] = \"Tom\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Arrays.fill() \u65b9\u6cd5<\/strong><\/p>\n<pre>String[] names = new String[3];\nArrays.fill(names, \"Unknown\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u6d41\u521d\u59cb\u5316<\/strong><\/p>\n<pre>String[] names = Stream.of(\"John\", \"Jane\", \"Tom\").toArray(String[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u4f7f\u7528 List \u521d\u59cb\u5316<\/strong><\/p>\n<pre>List&lt;String&gt; names = List.of(\"John\", \"Jane\", \"Tom\");\nString[] array = names.toArray(new String[names.size()]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>6. \u4f7f\u7528 Java 10+ \u7684 var \u5173\u952e\u5b57<\/strong><\/p>\n<pre>var names = new String[] {\"John\", \"Jane\", \"Tom\"};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u60c5\u51b5\uff1a<\/strong><\/p>\n<ul>\n<li>\u76f4\u63a5\u521d\u59cb\u5316\u9002\u7528\u4e8e\u5df2\u77e5\u6570\u7ec4\u5143\u7d20\u7684\u60c5\u51b5\u4e0b\u3002<\/li>\n<li>new \u5173\u952e\u5b57\u9002\u7528\u4e8e\u52a8\u6001\u521b\u5efa\u6570\u7ec4\u5e76\u9010\u4e2a\u8d4b\u503c\u5143\u7d20\u7684\u60c5\u51b5\u3002<\/li>\n<li>Arrays.fill() \u65b9\u6cd5\u9002\u7528\u4e8e\u5411\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8d4b\u503c\u76f8\u540c\u503c\u7684\u60c5\u51b5\u3002<\/li>\n<li>\u6d41\u548c List \u521d\u59cb\u5316\u65b9\u6cd5\u9002\u7528\u4e8e\u4ece\u5176\u4ed6\u6570\u636e\u6e90\u521b\u5efa\u6570\u7ec4\u7684\u60c5\u51b5\u3002<\/li>\n<li>var \u5173\u952e\u5b57\u662f\u4e3a\u4e86\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u9002\u7528\u4e8e\u6570\u7ec4\u5143\u7d20\u7c7b\u578b\u53ef\u4ee5\u4ece\u521d\u59cb\u5316\u5668\u63a8\u65ad\u51fa\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u521d\u59cb\u5316string\u6570\u7ec4 java\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>\u5b57\u7b26\u4e32\u6570\u7ec4\u5728 java \u4e2d\u6709\u591a\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\uff1a\u76f4\u63a5\u521d\u59cb\u5316\uff1astring[] names = {&#8220;john&#8221;, &#8220;jane&#8221;, &#8220;tom&#8221;};\u4f7f\u7528 new \u5173\u952e\u5b57\uff1astring[] names = new string[3];\u4f7f\u7528 arrays.fill() \u65b9\u6cd5\uff1astring[] names = new string[3]; arrays.fill(names, &#8220;unknown&#8221;);\u4f7f\u7528\u6d41\u521d\u59cb\u5316\uff1astring[] names = stream.of(&#8220;john&#8221;, &#8220;jane&#8221;, &#8220;tom&#8221;). \u5982\u4f55\u521d\u59cb\u5316\uff1f \u5728 Java \u4e2d\uff0c\u5b57\u7b26\u4e32\u6570\u7ec4\u53ef\u4ee5\u6709\u591a\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\uff1a 1. \u76f4\u63a5\u521d\u59cb\u5316 String[] names = {&#8220;John&#8221;, &#8220;Jane&#8221;, &#8220;Tom&#8221;}; \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 new \u5173\u952e\u5b57 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] names = new String[3]; names[0] = &#8220;John&#8221;; names[1] = [&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-65888","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65888","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=65888"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65888\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}