{"id":66468,"date":"2025-05-03T14:49:29","date_gmt":"2025-05-03T06:49:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66468\/"},"modified":"2025-05-03T14:49:29","modified_gmt":"2025-05-03T06:49:29","slug":"java%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e9%81%8d%e5%8e%86%e8%be%93%e5%87%ba-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66468\/","title":{"rendered":"java\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u904d\u5386\u8f93\u51fa"},"content":{"rendered":"<blockquote><p>\n  \u89e3\u51b3\u65b9\u6cd5\uff1a\u5d4c\u5957\u5faa\u73af\u589e\u5f3a\u578b for \u5faa\u73afstream api\u8be6\u7ec6\u4fe1\u606f\uff1a\u5d4c\u5957\u5faa\u73af\u9010\u884c\u6d4f\u89c8\u6570\u7ec4\uff0c\u6253\u5370\u6bcf\u4e2a\u5143\u7d20\u3002\u589e\u5f3a\u578b for \u5faa\u73af\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u904d\u5386\u6570\u7ec4\u3002stream api \u4f7f\u7528 flatmaptoint() \u65b9\u6cd5\u5e73\u5766\u5316\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528 foreach() \u65b9\u6cd5\u6253\u5370\u6bcf\u4e2a\u5143\u7d20\u3002\u6700\u4f73\u65b9\u6cd5\u53d6\u51b3\u4e8e\u7528\u4f8b\u548c\u504f\u597d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111604220166034.jpg\" class=\"aligncenter\" title=\"java\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u904d\u5386\u8f93\u51fa\u63d2\u56fe\" alt=\"java\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u904d\u5386\u8f93\u51fa\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e8c\u7ef4\u6570\u7ec4\u904d\u5386<\/strong><\/p>\n<p>\u904d\u5386 Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5d4c\u5957\u5faa\u73af<\/strong><\/p>\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};\n\nfor (int i = 0; i &lt; arr.length; i++) {\n    for (int j = 0; j &lt; arr[i].length; j++) {\n        System.out.println(arr[i][j]);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u4ee3\u7801\u5c06\u6309\u884c\u904d\u5386\u6570\u7ec4\uff0c\u6253\u5370\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<p><strong>\u589e\u5f3a\u578b for \u5faa\u73af<\/strong><\/p>\n<p>\u589e\u5f3a\u578b for \u5faa\u73af\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n<pre>int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};\n\nfor (int[] row : arr) {\n    for (int element : row) {\n        System.out.println(element);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u4ee3\u7801\u5c06\u4ee5\u4e0e\u5d4c\u5957\u5faa\u73af\u76f8\u540c\u7684\u65b9\u5f0f\u904d\u5386\u6570\u7ec4\u3002<\/p>\n<p><strong>Stream API<\/strong><\/p>\n<p>Java 8 \u5f15\u5165\u4e86 Stream API\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u51fd\u6570\u5f0f\u7684\u904d\u5386\u65b9\u5f0f\uff1a<\/p>\n<pre>int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};\n\nArrays.stream(arr)\n        .flatMapToInt(Arrays::stream)\n        .forEach(System.out::println);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u4ee3\u7801\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a IntStream\uff0c\u7136\u540e\u4f7f\u7528 flatMapToInt() \u65b9\u6cd5\u5bf9\u5176\u8fdb\u884c\u6241\u5e73\u5316\uff0c\u4ece\u800c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5143\u7d20\u7684\u5355\u4e00\u6d41\u3002\u7136\u540e\uff0c\u4f7f\u7528 forEach() \u65b9\u6cd5\u5bf9\u6d41\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u6307\u5b9a\u7684\u64cd\u4f5c\uff08\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u662f\u6253\u5370\u5143\u7d20\uff09\u3002<\/p>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u54ea\u79cd\u6700\u9002\u5408\u53d6\u51b3\u4e8e\u7279\u5b9a\u7684\u7528\u4f8b\u548c\u504f\u597d\u3002\u5d4c\u5957\u5faa\u73af\u662f\u6700\u7b80\u5355\u7684\uff0c\u800c Stream API \u63d0\u4f9b\u4e86\u6700\u51fd\u6570\u5f0f\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u904d\u5386\u8f93\u51fa\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>\u89e3\u51b3\u65b9\u6cd5\uff1a\u5d4c\u5957\u5faa\u73af\u589e\u5f3a\u578b for \u5faa\u73afstream api\u8be6\u7ec6\u4fe1\u606f\uff1a\u5d4c\u5957\u5faa\u73af\u9010\u884c\u6d4f\u89c8\u6570\u7ec4\uff0c\u6253\u5370\u6bcf\u4e2a\u5143\u7d20\u3002\u589e\u5f3a\u578b for \u5faa\u73af\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u904d\u5386\u6570\u7ec4\u3002stream api \u4f7f\u7528 flatmaptoint() \u65b9\u6cd5\u5e73\u5766\u5316\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528 foreach() \u65b9\u6cd5\u6253\u5370\u6bcf\u4e2a\u5143\u7d20\u3002\u6700\u4f73\u65b9\u6cd5\u53d6\u51b3\u4e8e\u7528\u4f8b\u548c\u504f\u597d\u3002 Java \u4e8c\u7ef4\u6570\u7ec4\u904d\u5386 \u904d\u5386 Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002 \u5d4c\u5957\u5faa\u73af \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; for (int i = 0; i &lt; arr.length; i++) { for (int j = 0; j &lt; arr[i].length; j++) { System.out.println(arr[i][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-66468","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66468","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=66468"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66468\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}