{"id":65492,"date":"2025-05-03T12:30:34","date_gmt":"2025-05-03T04:30:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65492\/"},"modified":"2025-05-03T12:30:34","modified_gmt":"2025-05-03T04:30:34","slug":"java%e6%80%8e%e4%b9%88%e7%bb%99%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84%e8%b5%8b%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65492\/","title":{"rendered":"java\u600e\u4e48\u7ed9\u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u7ed9\u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c\u6709\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u76f4\u63a5\u8d4b\u503c\u4f7f\u7528\u5faa\u73af\u4f7f\u7528 arrays.fill() \u65b9\u6cd5\u4f7f\u7528 collections.ncopies() \u65b9\u6cd5\u4f7f\u7528 stream api\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111302183883557.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u7ed9\u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c\u63d2\u56fe\" alt=\"java\u600e\u4e48\u7ed9\u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u7ed9 Java \u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u7ed9\u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u76f4\u63a5\u8d4b\u503c<\/strong><\/p>\n<p>\u76f4\u63a5\u8d4b\u503c\u662f\u7ed9\u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[] names = {\"John\", \"Mary\", \"Bob\"};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e00\u6b21\u53ea\u80fd\u8d4b\u503c\u4e00\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u9700\u8981\u8d4b\u503c\u591a\u4e2a\u5143\u7d20\uff0c\u9700\u8981\u91cd\u590d\u6b64\u64cd\u4f5c\u3002<\/p>\n<p><strong>2. \u4f7f\u7528\u5faa\u73af<\/strong><\/p>\n<p>\u4f7f\u7528\u5faa\u73af\u53ef\u4ee5\u4e00\u6b21\u6027\u7ed9\u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c\u591a\u4e2a\u5143\u7d20\uff1a<\/p>\n<pre>String[] names = new String[3];\nfor (int i = 0; i &lt; names.length; i++) {\n    names[i] = \"Name\" + i;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Arrays.fill() \u65b9\u6cd5<\/strong><\/p>\n<p>Arrays.fill() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u5bf9\u8c61\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u586b\u5145\u4e3a\u6307\u5b9a\u503c\uff1a<\/p>\n<pre>String[] names = new String[3];\nArrays.fill(names, \"Default Name\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528 Collections.nCopies() \u65b9\u6cd5<\/strong><\/p>\n<p>Collections.nCopies() \u65b9\u6cd5\u53ef\u4ee5\u521b\u5efa\u5305\u542b\u6307\u5b9a\u503c n \u4e2a\u526f\u672c\u7684\u5bf9\u8c61\u6570\u7ec4\uff1a<\/p>\n<pre>String[] names = Collections.nCopies(3, \"John\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u4f7f\u7528 Stream API<\/strong><\/p>\n<p>Java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u63d0\u4f9b\u4e86 Stream API\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u7ed9\u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c\uff1a<\/p>\n<pre>String[] names = Stream.of(\"John\", \"Mary\", \"Bob\").toArray(String[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u7ed9\u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c\u53d6\u51b3\u4e8e\u7279\u5b9a\u573a\u666f\uff1a<\/p>\n<ul>\n<li> <strong>\u76f4\u63a5\u8d4b\u503c<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u9010\u4e2a\u8d4b\u503c\u7684\u60c5\u51b5\u3002<\/li>\n<li> <strong>\u4f7f\u7528\u5faa\u73af<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u6279\u91cf\u8d4b\u503c\u6216\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u8d4b\u503c\u7684\u60c5\u51b5\u3002<\/li>\n<li> <strong>\u4f7f\u7528 Arrays.fill() \u65b9\u6cd5<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u5c06\u6570\u7ec4\u6240\u6709\u5143\u7d20\u586b\u5145\u4e3a\u76f8\u540c\u503c\u7684\u60c5\u51b5\u3002<\/li>\n<li> <strong>\u4f7f\u7528 Collections.nCopies() \u65b9\u6cd5<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u521b\u5efa\u5305\u542b\u76f8\u540c\u503c\u591a\u4e2a\u526f\u672c\u7684\u6570\u7ec4\u7684\u60c5\u51b5\u3002<\/li>\n<li> <strong>\u4f7f\u7528 Stream API<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u4f7f\u7528\u6d41\u64cd\u4f5c\u5904\u7406\u6570\u7ec4\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u7ed9\u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c\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\u7ed9\u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c\u6709\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u76f4\u63a5\u8d4b\u503c\u4f7f\u7528\u5faa\u73af\u4f7f\u7528 arrays.fill() \u65b9\u6cd5\u4f7f\u7528 collections.ncopies() \u65b9\u6cd5\u4f7f\u7528 stream api \u5982\u4f55\u7ed9 Java \u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c \u5728 Java \u4e2d\uff0c\u7ed9\u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a 1. \u76f4\u63a5\u8d4b\u503c \u76f4\u63a5\u8d4b\u503c\u662f\u7ed9\u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] names = {&#8220;John&#8221;, &#8220;Mary&#8221;, &#8220;Bob&#8221;}; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u79cd\u65b9\u6cd5\u4e00\u6b21\u53ea\u80fd\u8d4b\u503c\u4e00\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u9700\u8981\u8d4b\u503c\u591a\u4e2a\u5143\u7d20\uff0c\u9700\u8981\u91cd\u590d\u6b64\u64cd\u4f5c\u3002 2. \u4f7f\u7528\u5faa\u73af \u4f7f\u7528\u5faa\u73af\u53ef\u4ee5\u4e00\u6b21\u6027\u7ed9\u5bf9\u8c61\u6570\u7ec4\u8d4b\u503c\u591a\u4e2a\u5143\u7d20\uff1a String[] names = new String[3]; for (int i = 0; i &lt; names.length; i++) { names[i] = &#8220;Name&#8221; + i; } \u767b\u5f55\u540e\u590d\u5236 3. \u4f7f\u7528 Arrays.fill() [&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-65492","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65492","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=65492"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65492\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}