{"id":34589,"date":"2024-11-26T11:26:52","date_gmt":"2024-11-26T03:26:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/34589\/"},"modified":"2024-11-26T11:26:52","modified_gmt":"2024-11-26T03:26:52","slug":"java%e6%80%8e%e4%b9%88%e5%be%80%e6%95%b0%e7%bb%84%e9%87%8c%e9%9d%a2%e5%8a%a0%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/34589\/","title":{"rendered":"java\u600e\u4e48\u5f80\u6570\u7ec4\u91cc\u9762\u52a0\u6570\u5b57"},"content":{"rendered":"<blockquote><p>\n  \u5f80 java \u6570\u7ec4\u6dfb\u52a0\u6570\u5b57\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u76f4\u63a5\u521d\u59cb\u5316\u5143\u7d20\u4f7f\u7528\u6570\u7ec4\u957f\u5ea6\u548c\u7d22\u5f15\u4f7f\u7528 arrays.fill() \u65b9\u6cd5\u4f7f\u7528\u589e\u5f3a\u7684 for \u5faa\u73af\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/02\/2024110217244972485.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5f80\u6570\u7ec4\u91cc\u9762\u52a0\u6570\u5b57\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5f80\u6570\u7ec4\u91cc\u9762\u52a0\u6570\u5b57\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5f80 Java \u6570\u7ec4\u4e2d\u6dfb\u52a0\u6570\u5b57<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u521d\u59cb\u5316<\/strong><\/p>\n<p>\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u5728\u6570\u7ec4\u58f0\u660e\u65f6\u521d\u59cb\u5316\u5143\u7d20\uff1a<\/p>\n<pre>int[] array = {1, 2, 3, 4, 5};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u6570\u7ec4\u957f\u5ea6\u548c\u7d22\u5f15<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5982\u679c\u6570\u7ec4\u957f\u5ea6\u5df2\u77e5\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u957f\u5ea6\u548c\u7d22\u5f15\u6765\u6dfb\u52a0\u6570\u5b57\uff1a<\/p>\n<pre>int[] array = new int[5];\narray[0] = 1;\narray[1] = 2;\narray[2] = 3;\narray[3] = 4;\narray[4] = 5;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Arrays.fill() \u65b9\u6cd5<\/strong><\/p>\n<p>Arrays.fill() \u65b9\u6cd5\u53ef\u7528\u4e8e\u7528\u7279\u5b9a\u503c\u586b\u5145\u6574\u4e2a\u6570\u7ec4\u6216\u5176\u4e00\u90e8\u5206\uff1a<\/p>\n<pre>int[] array = new int[5];\nArrays.fill(array, 1); \/\/ \u586b\u5145\u6574\u4e2a\u6570\u7ec4\u4e3a 1\nArrays.fill(array, 1, 3, 2); \/\/ \u586b\u5145\u6570\u7ec4\u4e0b\u6807 1 \u5230 2 \u7684\u90e8\u5206\u4e3a 2<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u589e\u5f3a\u7684 for \u5faa\u73af<\/strong><\/p>\n<p>\u589e\u5f3a\u7684 for \u5faa\u73af\u53ef\u7528\u4e8e\u904d\u5386\u6570\u7ec4\u5e76\u6dfb\u52a0\u6570\u5b57\uff1a<\/p>\n<pre>int[] array = new int[5];\nint value = 1;\nfor (int element : array) {\n    element = value;\n    value++;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 lambda\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>lambda \u8868\u8fbe\u5f0f\u53ef\u7528\u4e8e\u901a\u8fc7 Stream API \u6765\u7b80\u6d01\u5730\u6dfb\u52a0\u6570\u5b57\uff1a<\/p>\n<pre>int[] array = new int[5];\nIntStream.iterate(1, i -&gt; i + 1)\n        .limit(array.length)\n        .forEach(i -&gt; array[i - 1] = i);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5f80\u6570\u7ec4\u91cc\u9762\u52a0\u6570\u5b57\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>\u5f80 java \u6570\u7ec4\u6dfb\u52a0\u6570\u5b57\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u76f4\u63a5\u521d\u59cb\u5316\u5143\u7d20\u4f7f\u7528\u6570\u7ec4\u957f\u5ea6\u548c\u7d22\u5f15\u4f7f\u7528 arrays.fill() \u65b9\u6cd5\u4f7f\u7528\u589e\u5f3a\u7684 for \u5faa\u73af\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f \u5982\u4f55\u5f80 Java \u6570\u7ec4\u4e2d\u6dfb\u52a0\u6570\u5b57 \u76f4\u63a5\u521d\u59cb\u5316 \u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u5728\u6570\u7ec4\u58f0\u660e\u65f6\u521d\u59cb\u5316\u5143\u7d20\uff1a int[] array = {1, 2, 3, 4, 5}; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528\u6570\u7ec4\u957f\u5ea6\u548c\u7d22\u5f15 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5982\u679c\u6570\u7ec4\u957f\u5ea6\u5df2\u77e5\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u957f\u5ea6\u548c\u7d22\u5f15\u6765\u6dfb\u52a0\u6570\u5b57\uff1a int[] array = new int[5]; array[0] = 1; array[1] = 2; array[2] = 3; array[3] = 4; array[4] = 5; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 Arrays.fill() \u65b9\u6cd5 Arrays.fill() \u65b9\u6cd5\u53ef\u7528\u4e8e\u7528\u7279\u5b9a\u503c\u586b\u5145\u6574\u4e2a\u6570\u7ec4\u6216\u5176\u4e00\u90e8\u5206\uff1a int[] array = [&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-34589","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34589","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=34589"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34589\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=34589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=34589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=34589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}