{"id":64609,"date":"2025-05-03T13:50:37","date_gmt":"2025-05-03T05:50:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64609\/"},"modified":"2025-05-03T13:50:37","modified_gmt":"2025-05-03T05:50:37","slug":"java%e5%8f%98%e9%87%8f%e6%80%8e%e4%b9%88%e6%8f%90%e4%be%9b%e6%95%b0%e7%bb%84%e5%88%9d%e5%a7%8b%e5%8c%96%e6%93%8d%e4%bd%9c-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64609\/","title":{"rendered":"java\u53d8\u91cf\u600e\u4e48\u63d0\u4f9b\u6570\u7ec4\u521d\u59cb\u5316\u64cd\u4f5c"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u7684\u53d8\u91cf\u53ef\u4ee5\u63d0\u4f9b\u6570\u7ec4\u7684\u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u5177\u4f53\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528\u82b1\u62ec\u53f7 {} \u6765\u6307\u5b9a\u6570\u7ec4\u5143\u7d20\uff1b\u4f7f\u7528 new \u5173\u952e\u5b57\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\u5e76\u521d\u59cb\u5316\u4e3a\u9ed8\u8ba4\u503c\uff1b\u4f7f\u7528 arrays.fill() \u65b9\u6cd5\u586b\u5145\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff1b\u4f7f\u7528 arrayinitializer \u8bed\u6cd5\u5728\u6570\u636e\u7c7b\u578b\u540e\u76f4\u63a5\u6307\u5b9a\u6570\u7ec4\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/04\/2024110403273020609.jpg\" class=\"aligncenter\" title=\"java\u53d8\u91cf\u600e\u4e48\u63d0\u4f9b\u6570\u7ec4\u521d\u59cb\u5316\u64cd\u4f5c\u63d2\u56fe\" alt=\"java\u53d8\u91cf\u600e\u4e48\u63d0\u4f9b\u6570\u7ec4\u521d\u59cb\u5316\u64cd\u4f5c\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u53d8\u91cf\u5982\u4f55\u63d0\u4f9b\u6570\u7ec4\u7684\u521d\u59cb\u5316\u64cd\u4f5c<\/strong><\/p>\n<p>Java \u4e2d\uff0c\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u521d\u59cb\u5316\u64cd\u4f5c\u6765\u8d4b\u503c\u3002\u5bf9\u4e8e\u6570\u7ec4\u53d8\u91cf\uff0c\u521d\u59cb\u5316\u64cd\u4f5c\u53ef\u4ee5\u4e3a\u5176\u5206\u914d\u4e00\u4e2a\u5305\u542b\u7279\u5b9a\u5143\u7d20\u7684\u6570\u7ec4\u3002<\/p>\n<p><strong>\u4f7f\u7528\u82b1\u62ec\u53f7 {}<\/strong><\/p>\n<p>\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u82b1\u62ec\u53f7 {} \u6765\u6307\u5b9a\u6570\u7ec4\u5143\u7d20\u3002\u82b1\u62ec\u53f7\u5185\u90e8\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u7528\u9017\u53f7\u5206\u9694\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] numbers = {1, 2, 3, 4, 5};\nString[] names = {\"John\", \"Mary\", \"Bob\", \"Alice\"};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 new \u5173\u952e\u5b57<\/strong><\/p>\n<p>new \u5173\u952e\u5b57\u4e5f\u53ef\u4ee5\u7528\u6765\u521d\u59cb\u5316\u6570\u7ec4\u3002\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u6307\u5b9a\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u5176\u5404\u5143\u7d20\u521d\u59cb\u5316\u4e3a\u9ed8\u8ba4\u503c\uff08\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u4e3a 0\uff0c\u5bf9\u4e8e\u5bf9\u8c61\u7c7b\u578b\u4e3a null\uff09\u3002<\/p>\n<pre>int[] numbers = new int[5]; \/\/ \u521d\u59cb\u5316\u4e3a [0, 0, 0, 0, 0]\nString[] names = new String[4]; \/\/ \u521d\u59cb\u5316\u4e3a [null, null, null, null]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Arrays.fill() \u65b9\u6cd5<\/strong><\/p>\n<p>Arrays \u7c7b\u63d0\u4f9b\u4e86 fill() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u586b\u5145\u4e3a\u6307\u5b9a\u503c\u3002<\/p>\n<pre>int[] numbers = new int[5];\nArrays.fill(numbers, 10); \/\/ \u521d\u59cb\u5316\u4e3a [10, 10, 10, 10, 10]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 ArrayInitializer \u8bed\u6cd5<\/strong><\/p>\n<p>Java 9 \u5f15\u5165\u4e86 ArrayInitializer \u8bed\u6cd5\uff0c\u8fd9\u662f\u4e00\u79cd\u7b80\u5316\u6570\u7ec4\u521d\u59cb\u5316\u7684\u8bed\u6cd5\u7cd6\u3002\u5b83\u5141\u8bb8\u5728\u6570\u636e\u7c7b\u578b\u4e4b\u540e\u76f4\u63a5\u6307\u5b9a\u6570\u7ec4\u5143\u7d20\uff0c\u800c\u4e0d\u5fc5\u4f7f\u7528\u82b1\u62ec\u53f7\u3002<\/p>\n<pre>int[] numbers = {1, 2, 3, 4, 5}; \/\/ \u4e0e\u4f7f\u7528\u82b1\u62ec\u53f7\u76f8\u540c\nString[] names = {\"John\", \"Mary\", \"Bob\", \"Alice\"};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u6570\u7ec4\u521d\u59cb\u5316\u64cd\u4f5c\u5728\u5b9a\u4e49\u53d8\u91cf\u65f6\u8fdb\u884c\u3002<\/li>\n<li>\u6570\u7ec4\u5143\u7d20\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u57fa\u672c\u7c7b\u578b\u3001\u5bf9\u8c61\u7c7b\u578b\u548c\u6570\u7ec4\u7c7b\u578b\u3002<\/li>\n<li>\u6570\u7ec4\u4e00\u65e6\u521d\u59cb\u5316\uff0c\u5176\u957f\u5ea6\u5c31\u4e0d\u80fd\u66f4\u6539\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u53d8\u91cf\u600e\u4e48\u63d0\u4f9b\u6570\u7ec4\u521d\u59cb\u5316\u64cd\u4f5c\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>java \u4e2d\u7684\u53d8\u91cf\u53ef\u4ee5\u63d0\u4f9b\u6570\u7ec4\u7684\u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u5177\u4f53\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528\u82b1\u62ec\u53f7 {} \u6765\u6307\u5b9a\u6570\u7ec4\u5143\u7d20\uff1b\u4f7f\u7528 new \u5173\u952e\u5b57\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\u5e76\u521d\u59cb\u5316\u4e3a\u9ed8\u8ba4\u503c\uff1b\u4f7f\u7528 arrays.fill() \u65b9\u6cd5\u586b\u5145\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff1b\u4f7f\u7528 arrayinitializer \u8bed\u6cd5\u5728\u6570\u636e\u7c7b\u578b\u540e\u76f4\u63a5\u6307\u5b9a\u6570\u7ec4\u5143\u7d20\u3002 Java \u53d8\u91cf\u5982\u4f55\u63d0\u4f9b\u6570\u7ec4\u7684\u521d\u59cb\u5316\u64cd\u4f5c Java \u4e2d\uff0c\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u521d\u59cb\u5316\u64cd\u4f5c\u6765\u8d4b\u503c\u3002\u5bf9\u4e8e\u6570\u7ec4\u53d8\u91cf\uff0c\u521d\u59cb\u5316\u64cd\u4f5c\u53ef\u4ee5\u4e3a\u5176\u5206\u914d\u4e00\u4e2a\u5305\u542b\u7279\u5b9a\u5143\u7d20\u7684\u6570\u7ec4\u3002 \u4f7f\u7528\u82b1\u62ec\u53f7 {} \u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u82b1\u62ec\u53f7 {} \u6765\u6307\u5b9a\u6570\u7ec4\u5143\u7d20\u3002\u82b1\u62ec\u53f7\u5185\u90e8\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u7528\u9017\u53f7\u5206\u9694\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] numbers = {1, 2, 3, 4, 5}; String[] names = {&#8220;John&#8221;, &#8220;Mary&#8221;, &#8220;Bob&#8221;, &#8220;Alice&#8221;}; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 new \u5173\u952e\u5b57 new \u5173\u952e\u5b57\u4e5f\u53ef\u4ee5\u7528\u6765\u521d\u59cb\u5316\u6570\u7ec4\u3002\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u6307\u5b9a\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u5176\u5404\u5143\u7d20\u521d\u59cb\u5316\u4e3a\u9ed8\u8ba4\u503c\uff08\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u4e3a 0\uff0c\u5bf9\u4e8e\u5bf9\u8c61\u7c7b\u578b\u4e3a null\uff09\u3002 int[] numbers = new int[5]; \/\/ \u521d\u59cb\u5316\u4e3a [0, 0, 0, 0, 0] [&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-64609","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64609","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=64609"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64609\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}