{"id":66982,"date":"2025-05-03T15:28:18","date_gmt":"2025-05-03T07:28:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66982\/"},"modified":"2025-05-03T15:28:18","modified_gmt":"2025-05-03T07:28:18","slug":"java%e6%80%8e%e4%b9%88%e7%bb%99%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e5%88%86%e9%85%8d%e5%85%83%e7%b4%a0-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66982\/","title":{"rendered":"java\u600e\u4e48\u7ed9\u4e8c\u7ef4\u6570\u7ec4\u5206\u914d\u5143\u7d20"},"content":{"rendered":"<blockquote><p>\n  \u5206\u914d java \u4e8c\u7ef4\u6570\u7ec4\u5143\u7d20\u7684\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u4e24\u4e2a for \u5faa\u73af\uff1b\u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\uff1b\u4f7f\u7528 stream api\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111721514128729.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u7ed9\u4e8c\u7ef4\u6570\u7ec4\u5206\u914d\u5143\u7d20\u63d2\u56fe\" alt=\"java\u600e\u4e48\u7ed9\u4e8c\u7ef4\u6570\u7ec4\u5206\u914d\u5143\u7d20\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u7ed9 Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u5206\u914d\u5143\u7d20<\/strong><\/p>\n<p><strong>\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p><strong>1. \u4f7f\u7528\u4e24\u4e2a for \u5faa\u73af<\/strong><\/p>\n<pre>int[][] array = new int[rows][cols];\n\nfor (int i = 0; i &lt; rows; i++) {\n    for (int j = 0; j &lt; cols; j++) {\n        array[i][j] = value;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Stream API<\/strong><\/p>\n<pre>int[][] array = IntStream.range(0, rows)\n    .mapToObj(i -&gt; IntStream.range(0, cols).toArray())\n    .toArray(int[][]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u7ed9\u4e00\u4e2a 3&#215;3 \u7684\u4e8c\u7ef4\u6570\u7ec4\u5206\u914d\u5143\u7d20\uff1a<\/p>\n<pre>int[][] array = new int[3][3];\n\nfor (int i = 0; i &lt; 3; i++) {\n    for (int j = 0; j &lt; 3; j++) {\n        array[i][j] = i * 3 + j;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8f93\u51fa\u6570\u7ec4\uff1a<\/p>\n<pre>[[0, 1, 2], [3, 4, 5], [6, 7, 8]]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u7ed9\u4e8c\u7ef4\u6570\u7ec4\u5206\u914d\u5143\u7d20\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>\u5206\u914d java \u4e8c\u7ef4\u6570\u7ec4\u5143\u7d20\u7684\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u4e24\u4e2a for \u5faa\u73af\uff1b\u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\uff1b\u4f7f\u7528 stream api\u3002 \u5982\u4f55\u7ed9 Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u5206\u914d\u5143\u7d20 \u65b9\u6cd5\uff1a 1. \u4f7f\u7528\u4e24\u4e2a for \u5faa\u73af int[][] array = new int[rows][cols]; for (int i = 0; i &lt; rows; i++) { for (int j = 0; j &lt; cols; j++) { array[i][j] = value; } } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[][] array = {{1, 2, 3}, [&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-66982","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66982","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=66982"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66982\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}