{"id":34751,"date":"2024-11-26T14:08:39","date_gmt":"2024-11-26T06:08:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/34751\/"},"modified":"2024-11-26T14:08:39","modified_gmt":"2024-11-26T06:08:39","slug":"java%e9%81%8d%e5%8e%86%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/34751\/","title":{"rendered":"java\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  java \u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\uff1a\u5d4c\u5957\u5faa\u73af\u3001\u589e\u5f3a\u578b for \u5faa\u73af\u548c stream api\u3002\u5d4c\u5957\u5faa\u73af\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u4f7f\u7528\u4e24\u4e2a\u5faa\u73af\u904d\u5386\u884c\u548c\u5217\uff1b\u589e\u5f3a\u578b for \u5faa\u73af\u4f7f\u7528\u5d4c\u5957\u7684\u589e\u5f3a\u578b for \u5faa\u73af\u904d\u5386\u6bcf\u4e00\u884c\u4e2d\u7684\u5143\u7d20\uff1bstream api \u4f7f\u7528 flatmap() \u548c foreach() \u65b9\u6cd5\u5c06\u4e8c\u7ef4\u6570\u7ec4\u5e73\u5c55\u4e3a\u4e00\u7ef4\u6d41\u5e76\u904d\u5386\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110302244916467.jpg\" class=\"aligncenter\" title=\"java\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"java\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u6307\u5357<\/strong><\/p>\n<p>Java \u4e2d\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u60a8\u5e0c\u671b\u5982\u4f55\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u4e09\u79cd\u4e3b\u8981\u904d\u5386\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u5d4c\u5957\u5faa\u73af<\/strong><\/p>\n<p>\u5d4c\u5957\u5faa\u73af\u662f\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u6700\u57fa\u7840\u4e5f\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u5b83\u6d89\u53ca\u4e24\u4e2a\u5faa\u73af\uff1a\u4e00\u4e2a\u904d\u5386\u884c\uff0c\u53e6\u4e00\u4e2a\u904d\u5386\u5217\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[][] array = {\n        {1, 2, 3},\n        {4, 5, 6},\n        {7, 8, 9}\n    };\n\nfor (int i = 0; i &lt; array.length; i++) {\n    for (int j = 0; j &lt; array[i].length; j++) {\n        System.out.print(array[i][j] + \" \");\n    }\n    System.out.println();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>1 2 3\n4 5 6\n7 8 9<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \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\u3002\u5b83\u4f7f\u7528\u4e00\u4e2a\u5d4c\u5957\u7684\u589e\u5f3a\u578b for \u5faa\u73af\u6765\u904d\u5386\u6bcf\u4e00\u884c\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<pre>for (int[] row : array) {\n    for (int element : row) {\n        System.out.print(element + \" \");\n    }\n    System.out.println();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8f93\u51fa\u4e0e\u5d4c\u5957\u5faa\u73af\u76f8\u540c\u3002<\/p>\n<p><strong>3. Stream API<\/strong><\/p>\n<p>Java 8 \u4e2d\u5f15\u5165\u7684 Stream API \u63d0\u4f9b\u4e86\u53e6\u4e00\u79cd\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u7684\u51fd\u6570\u5f0f\u65b9\u6cd5\u3002\u5b83\u4f7f\u7528 flatMap() \u65b9\u6cd5\u5c06\u4e8c\u7ef4\u6570\u7ec4\u5e73\u5c55\u4e3a\u4e00\u7ef4\u6d41\uff0c\u7136\u540e\u4f7f\u7528 forEach() \u65b9\u6cd5\u904d\u5386\u6d41\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<pre>Arrays.stream(array)\n        .flatMapToInt(Arrays::stream)\n        .forEach(System.out::println);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>1\n2\n3\n4\n5\n6\n7\n8\n9<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u5199\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 \u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\uff1a\u5d4c\u5957\u5faa\u73af\u3001\u589e\u5f3a\u578b for \u5faa\u73af\u548c stream api\u3002\u5d4c\u5957\u5faa\u73af\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u4f7f\u7528\u4e24\u4e2a\u5faa\u73af\u904d\u5386\u884c\u548c\u5217\uff1b\u589e\u5f3a\u578b for \u5faa\u73af\u4f7f\u7528\u5d4c\u5957\u7684\u589e\u5f3a\u578b for \u5faa\u73af\u904d\u5386\u6bcf\u4e00\u884c\u4e2d\u7684\u5143\u7d20\uff1bstream api \u4f7f\u7528 flatmap() \u548c foreach() \u65b9\u6cd5\u5c06\u4e8c\u7ef4\u6570\u7ec4\u5e73\u5c55\u4e3a\u4e00\u7ef4\u6d41\u5e76\u904d\u5386\u3002 Java \u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u6307\u5357 Java \u4e2d\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u60a8\u5e0c\u671b\u5982\u4f55\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u4e09\u79cd\u4e3b\u8981\u904d\u5386\u65b9\u6cd5\uff1a 1. \u5d4c\u5957\u5faa\u73af \u5d4c\u5957\u5faa\u73af\u662f\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u6700\u57fa\u7840\u4e5f\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u5b83\u6d89\u53ca\u4e24\u4e2a\u5faa\u73af\uff1a\u4e00\u4e2a\u904d\u5386\u884c\uff0c\u53e6\u4e00\u4e2a\u904d\u5386\u5217\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; for (int i = 0; i &lt; array.length; i++) { for (int j = 0; j &lt; [&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-34751","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34751","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=34751"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34751\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=34751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=34751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=34751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}