{"id":35513,"date":"2024-11-26T17:53:24","date_gmt":"2024-11-26T09:53:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35513\/"},"modified":"2024-11-26T17:53:24","modified_gmt":"2024-11-26T09:53:24","slug":"java%e6%80%8e%e4%b9%88%e7%bb%99%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e6%89%b9%e9%87%8f%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35513\/","title":{"rendered":"java\u600e\u4e48\u7ed9\u4e8c\u7ef4\u6570\u7ec4\u6279\u91cf\u8d4b\u503c"},"content":{"rendered":"<blockquote><p>\n  java \u4e8c\u7ef4\u6570\u7ec4\u6279\u91cf\u8d4b\u503c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a1. \u58f0\u660e\u5e76\u521d\u59cb\u5316\u6570\u7ec4\uff1b2. \u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8d4b\u503c\uff1b3. \u4f7f\u7528 arrays.fill() \u65b9\u6cd5\uff1b4. \u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\uff08java 8 \u53ca\u4ee5\u4e0a\u7248\u672c\uff09\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/04\/2024110404362533729.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u7ed9\u4e8c\u7ef4\u6570\u7ec4\u6279\u91cf\u8d4b\u503c\u63d2\u56fe\" alt=\"java\u600e\u4e48\u7ed9\u4e8c\u7ef4\u6570\u7ec4\u6279\u91cf\u8d4b\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u7ed9 Java \u4e8c\u7ef4\u6570\u7ec4\u6279\u91cf\u8d4b\u503c<\/strong><\/p>\n<p>\u7ed9 Java \u4e8c\u7ef4\u6570\u7ec4\u6279\u91cf\u8d4b\u503c\u7684\u8fc7\u7a0b\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u58f0\u660e\u548c\u521d\u59cb\u5316\u6570\u7ec4<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u58f0\u660e\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[][] array = new int[3][4];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b 3 \u884c\u548c 4 \u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<p><strong>2. \u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8d4b\u503c<\/strong><\/p>\n<p>\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u9010\u884c\u9010\u5217\u5730\u7ed9\u6570\u7ec4\u5143\u7d20\u8d4b\u503c\u3002\u5916\u5c42\u5faa\u73af\u904d\u5386\u884c\uff0c\u5185\u5c42\u5faa\u73af\u904d\u5386\u5217\uff1a<\/p>\n<pre>for (int i = 0; i &lt; array.length; i++) {\n    for (int j = 0; j &lt; array[i].length; j++) {\n        array[i][j] = value;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>&#8220;value&#8221; \u662f\u8981\u8d4b\u7ed9\u5143\u7d20\u7684\u503c\u3002<\/p>\n<p><strong>3. \u4f7f\u7528 Arrays.fill<\/strong><\/p>\n<p>\u5bf9\u4e8e\u66f4\u7b80\u5355\u7684\u8d4b\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.fill() \u65b9\u6cd5\u3002\u5b83\u4f1a\u5c06\u6570\u7ec4\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6240\u6709\u5143\u7d20\u90fd\u586b\u5145\u4e3a\u7ed9\u5b9a\u503c\uff1a<\/p>\n<pre>Arrays.fill(array, value);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u5bf9\u4e8e Java 8 \u6216\u66f4\u9ad8\u7248\u672c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f\u6765\u7b80\u5316\u8d4b\u503c\u8fc7\u7a0b\uff1a<\/p>\n<pre>IntStream.range(0, array.length).forEach(i -&gt; IntStream.range(0, array[i].length).forEach(j -&gt; array[i][j] = value));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u7ed9\u4e8c\u7ef4\u6570\u7ec4\u6279\u91cf\u8d4b\u503c\u4e3a 5\uff1a<\/p>\n<pre>int[][] array = new int[3][4];\n\nfor (int i = 0; i &lt; array.length; i++) {\n    for (int j = 0; j &lt; array[i].length; j++) {\n        array[i][j] = 5;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7ecf\u8fc7\u6b64\u8fc7\u7a0b\uff0c\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u5c06\u53d8\u4e3a 5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u7ed9\u4e8c\u7ef4\u6570\u7ec4\u6279\u91cf\u8d4b\u503c\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>java \u4e8c\u7ef4\u6570\u7ec4\u6279\u91cf\u8d4b\u503c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a1. \u58f0\u660e\u5e76\u521d\u59cb\u5316\u6570\u7ec4\uff1b2. \u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8d4b\u503c\uff1b3. \u4f7f\u7528 arrays.fill() \u65b9\u6cd5\uff1b4. \u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\uff08java 8 \u53ca\u4ee5\u4e0a\u7248\u672c\uff09\u3002 \u5982\u4f55\u7ed9 Java \u4e8c\u7ef4\u6570\u7ec4\u6279\u91cf\u8d4b\u503c \u7ed9 Java \u4e8c\u7ef4\u6570\u7ec4\u6279\u91cf\u8d4b\u503c\u7684\u8fc7\u7a0b\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u58f0\u660e\u548c\u521d\u59cb\u5316\u6570\u7ec4 \u9996\u5148\uff0c\u58f0\u660e\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[][] array = new int[3][4]; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b 3 \u884c\u548c 4 \u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002 2. \u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8d4b\u503c \u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u9010\u884c\u9010\u5217\u5730\u7ed9\u6570\u7ec4\u5143\u7d20\u8d4b\u503c\u3002\u5916\u5c42\u5faa\u73af\u904d\u5386\u884c\uff0c\u5185\u5c42\u5faa\u73af\u904d\u5386\u5217\uff1a for (int i = 0; i &lt; array.length; i++) { for (int j = 0; j &lt; array[i].length; j++) [&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-35513","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35513","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=35513"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35513\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}