{"id":39435,"date":"2024-11-26T14:21:23","date_gmt":"2024-11-26T06:21:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39435\/"},"modified":"2024-11-26T14:21:23","modified_gmt":"2024-11-26T06:21:23","slug":"java%e4%b8%ad%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39435\/","title":{"rendered":"java\u4e2d\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u6392\u5e8f"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u6392\u5e8f\u4e8c\u7ef4\u6570\u7ec4\u7684\u6b65\u9aa4\u4f9d\u6b21\u4e3a\uff1a1. \u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff1b2. \u4f7f\u7528 arrays.sort \u65b9\u6cd5\u5bf9\u4e00\u7ef4\u6570\u7ec4\u6392\u5e8f\uff1b3. \u6839\u636e\u539f\u59cb\u5f62\u72b6\u5c06\u6392\u5e8f\u7684\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u56de\u4e8c\u7ef4\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111610371742766.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u6392\u5e8f\u63d2\u56fe\" alt=\"java\u4e2d\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u6392\u5e8f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5bf9 Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f<\/strong><\/p>\n<p>Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u662f\u4e00\u4e2a\u5305\u542b\u4e00\u7ec4\u4e00\u7ef4\u6570\u7ec4\u7684\u6570\u7ec4\u3002\u8981\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u4f7f\u7528 Arrays.stream(arr) \u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6d41\u3002<\/li>\n<li>\u7136\u540e\uff0c\u4f7f\u7528 flatMapToInt(Arrays::stream) \u5c06\u6240\u6709\u4e00\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5c55\u5e73\u4e3a\u4e00\u4e2a\u6d41\u3002<\/li>\n<li>\u6700\u540e\uff0c\u4f7f\u7528 toArray() \u5c06\u6d41\u8f6c\u6362\u4e3a\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p><strong>2. \u6392\u5e8f\u4e00\u7ef4\u6570\u7ec4<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u53ef\u4ee5\u4f7f\u7528 Arrays.sort(arr) \u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<li>\u6392\u5e8f\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a Comparator \u53c2\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u6307\u5b9a\u6392\u5e8f\u89c4\u5219\u3002<\/li>\n<li>\u4f8b\u5982\uff0c\u8981\u6309\u5347\u5e8f\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528 Comparator.naturalOrder()\u3002\u8981\u6309\u964d\u5e8f\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528 Comparator.reverseOrder()\u3002<\/li>\n<\/ul>\n<p><strong>3. \u5c06\u6392\u5e8f\u7684\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/p>\n<ul>\n<li>\u8981\u5c06\u6392\u5e8f\u7684\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u56de\u4e8c\u7ef4\u6570\u7ec4\uff0c\u9700\u8981\u77e5\u9053\u5176\u539f\u59cb\u5f62\u72b6\u3002<\/li>\n<li>\u4f7f\u7528 Arrays.stream(originalArr) \u518d\u6b21\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4\u7684\u6d41\u3002<\/li>\n<li>\u7136\u540e\uff0c\u4f7f\u7528 mapToInt(arr -&gt; arr[0]).toArray() \u83b7\u53d6\u6bcf\u4e2a\u4e00\u7ef4\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u8fd9\u5c06\u521b\u5efa\u4e00\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u4e8c\u7ef4\u6570\u7ec4\u7684\u5bbd\u5ea6\u3002<\/li>\n<li>\u6700\u540e\uff0c\u4f7f\u7528 IntStream.range(0, height).mapToObj(i -&gt; Arrays.copyOfRange(sortedArr, i * width, i * width + width)) \u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4\u3002\u5176\u4e2d\uff0cheight \u662f\u4e8c\u7ef4\u6570\u7ec4\u7684\u9ad8\u5ea6\uff0cwidth \u662f\u5bbd\u5ea6\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>int[][] arr = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};\n\n\/\/ \u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\nint[] flattenedArr = Arrays.stream(arr).flatMapToInt(Arrays::stream).toArray();\n\n\/\/ \u6309\u5347\u5e8f\u6392\u5e8f\nArrays.sort(flattenedArr, Comparator.naturalOrder());\n\n\/\/ \u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\nint height = arr.length;\nint width = arr[0].length;\nint[][] sortedArr = IntStream.range(0, height).mapToObj(i -&gt; Arrays.copyOfRange(flattenedArr, i * width, i * width + width)).toArray(int[][]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u6392\u5e8f\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 \u4e2d\u6392\u5e8f\u4e8c\u7ef4\u6570\u7ec4\u7684\u6b65\u9aa4\u4f9d\u6b21\u4e3a\uff1a1. \u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff1b2. \u4f7f\u7528 arrays.sort \u65b9\u6cd5\u5bf9\u4e00\u7ef4\u6570\u7ec4\u6392\u5e8f\uff1b3. \u6839\u636e\u539f\u59cb\u5f62\u72b6\u5c06\u6392\u5e8f\u7684\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u56de\u4e8c\u7ef4\u6570\u7ec4\u3002 \u5982\u4f55\u5bf9 Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u662f\u4e00\u4e2a\u5305\u542b\u4e00\u7ec4\u4e00\u7ef4\u6570\u7ec4\u7684\u6570\u7ec4\u3002\u8981\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4 \u9996\u5148\uff0c\u4f7f\u7528 Arrays.stream(arr) \u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6d41\u3002 \u7136\u540e\uff0c\u4f7f\u7528 flatMapToInt(Arrays::stream) \u5c06\u6240\u6709\u4e00\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5c55\u5e73\u4e3a\u4e00\u4e2a\u6d41\u3002 \u6700\u540e\uff0c\u4f7f\u7528 toArray() \u5c06\u6d41\u8f6c\u6362\u4e3a\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\u3002 2. \u6392\u5e8f\u4e00\u7ef4\u6570\u7ec4 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u53ef\u4ee5\u4f7f\u7528 Arrays.sort(arr) \u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 \u6392\u5e8f\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a Comparator \u53c2\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u6307\u5b9a\u6392\u5e8f\u89c4\u5219\u3002 \u4f8b\u5982\uff0c\u8981\u6309\u5347\u5e8f\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528 Comparator.naturalOrder()\u3002\u8981\u6309\u964d\u5e8f\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528 Comparator.reverseOrder()\u3002 3. \u5c06\u6392\u5e8f\u7684\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4 \u8981\u5c06\u6392\u5e8f\u7684\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u56de\u4e8c\u7ef4\u6570\u7ec4\uff0c\u9700\u8981\u77e5\u9053\u5176\u539f\u59cb\u5f62\u72b6\u3002 \u4f7f\u7528 Arrays.stream(originalArr) \u518d\u6b21\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4\u7684\u6d41\u3002 \u7136\u540e\uff0c\u4f7f\u7528 mapToInt(arr -&gt; arr[0]).toArray() \u83b7\u53d6\u6bcf\u4e2a\u4e00\u7ef4\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u8fd9\u5c06\u521b\u5efa\u4e00\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u4e8c\u7ef4\u6570\u7ec4\u7684\u5bbd\u5ea6\u3002 \u6700\u540e\uff0c\u4f7f\u7528 IntStream.range(0, height).mapToObj(i -&gt; Arrays.copyOfRange(sortedArr, i * width, i [&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-39435","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39435","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=39435"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39435\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}