{"id":64587,"date":"2025-05-03T15:17:48","date_gmt":"2025-05-03T07:17:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64587\/"},"modified":"2025-05-03T15:17:48","modified_gmt":"2025-05-03T07:17:48","slug":"%e6%80%8e%e4%b9%88%e4%bd%bf%e6%95%b0%e7%bb%84%e5%88%9d%e5%a7%8b%e5%8c%96%e5%85%a8%e4%b8%ba%e9%9b%b6java-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64587\/","title":{"rendered":"\u600e\u4e48\u4f7f\u6570\u7ec4\u521d\u59cb\u5316\u5168\u4e3a\u96f6java"},"content":{"rendered":"<blockquote><p>\n  \u6709\u4e09\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06 java \u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u521d\u59cb\u5316\u4e3a\u96f6\uff1a\u4f7f\u7528 for \u5faa\u73af\u3002\u4f7f\u7528 arrays.fill() \u65b9\u6cd5\u3002\u4f7f\u7528 stream.generate() \u548c intstream.of()\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/04\/2024110402304411762.jpg\" class=\"aligncenter\" title=\"\u600e\u4e48\u4f7f\u6570\u7ec4\u521d\u59cb\u5316\u5168\u4e3a\u96f6java\u63d2\u56fe\" alt=\"\u600e\u4e48\u4f7f\u6570\u7ec4\u521d\u59cb\u5316\u5168\u4e3a\u96f6java\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f Java \u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u5168\u96f6<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u521d\u59cb\u5316\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u4e3a\u96f6\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 for \u5faa\u73af\uff1a<\/strong><\/p>\n<pre>int[] arr = new int[size];\nfor (int i = 0; i &lt; arr.length; i++) {\n    arr[i] = 0;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Arrays.fill() \u65b9\u6cd5\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] arr = new int[size];\nArrays.fill(arr, 0);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Stream.generate() \u548c IntStream.of()\uff1a<\/strong><\/p>\n<pre>int[] arr = IntStream.of(0).generate(() -&gt; 0).limit(size).toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>for \u5faa\u73af<\/strong>\uff1a\u9002\u7528\u4e8e\u8f83\u5c0f\u7684\u6570\u7ec4\uff0c\u56e0\u4e3a\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\u3002<\/li>\n<li> <strong>Arrays.fill() \u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u8f83\u5927\u7684\u6570\u7ec4\uff0c\u56e0\u4e3a\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(1)\u3002<\/li>\n<li> <strong>Stream.generate() \u548c IntStream.of()<\/strong>\uff1a\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u6cd5\u6765\u521d\u59cb\u5316\u6570\u7ec4\uff0c\u4f46\u5b83\u7684\u6548\u7387\u4f4e\u4e8e Arrays.fill() \u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u4ee5\u4e0a\u65b9\u6cd5\u5c06\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002<\/li>\n<li>\u5982\u679c\u8981\u5c06\u73b0\u6709\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u521d\u59cb\u5316\u4e3a\u96f6\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.fill() \u65b9\u6cd5\u6216\u663e\u5f0f\u5730\u904d\u5386\u6570\u7ec4\u5e76\u8bbe\u7f6e\u6bcf\u4e2a\u5143\u7d20\u4e3a\u96f6\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u4f7f\u6570\u7ec4\u521d\u59cb\u5316\u5168\u4e3a\u96f6java\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>\u6709\u4e09\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06 java \u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u521d\u59cb\u5316\u4e3a\u96f6\uff1a\u4f7f\u7528 for \u5faa\u73af\u3002\u4f7f\u7528 arrays.fill() \u65b9\u6cd5\u3002\u4f7f\u7528 stream.generate() \u548c intstream.of()\u3002 \u5982\u4f55\u4f7f Java \u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u5168\u96f6 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u521d\u59cb\u5316\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u4e3a\u96f6\uff1a 1. \u4f7f\u7528 for \u5faa\u73af\uff1a int[] arr = new int[size]; for (int i = 0; i &lt; arr.length; i++) { arr[i] = 0; } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 Arrays.fill() \u65b9\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] arr = new int[size]; Arrays.fill(arr, 0); \u767b\u5f55\u540e\u590d\u5236 3. \u4f7f\u7528 [&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-64587","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64587","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=64587"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64587\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}