{"id":66720,"date":"2025-05-03T11:55:09","date_gmt":"2025-05-03T03:55:09","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66720\/"},"modified":"2025-05-03T11:55:09","modified_gmt":"2025-05-03T03:55:09","slug":"%e5%af%b9%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e6%8e%92%e5%ba%8fjava-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66720\/","title":{"rendered":"\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u6392\u5e8fjava"},"content":{"rendered":"<blockquote><p>\n  \u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f\u9700\u5c06\u6570\u7ec4\u5c55\u5e73\u4e3a\u4e00\u7ef4\u6570\u7ec4\u6216\u8f6c\u7f6e\u540e\u6309\u884c\u6392\u5e8f\uff1a\u6309\u884c\u6392\u5e8f\uff1a\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\u5bf9\u5c55\u5e73\u7684\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u6309\u5217\u6392\u5e8f\uff1a\u8f6c\u7f6e\u6570\u7ec4\u540e\u6309\u884c\u6392\u5e8f\uff0c\u518d\u6062\u590d\u4e3a\u539f\u59cb\u5f62\u72b6\u3002\u81ea\u5b9a\u4e49\u6392\u5e8f\uff1a\u4f7f\u7528\u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668\u6309\u7279\u5b9a\u6761\u4ef6\u5bf9\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111619422426173.jpg\" class=\"aligncenter\" title=\"\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u6392\u5e8fjava\u63d2\u56fe\" alt=\"\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u6392\u5e8fjava\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f<\/strong><\/p>\n<p><strong>\u5165\u95e8<\/strong><\/p>\n<p>\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u662f\u6307\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u5bf9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u91cd\u65b0\u3002\u53ef\u4ee5\u6839\u636e\u884c\u6216\u5217\u8fdb\u884c\u6392\u5e8f\uff0c\u6392\u5e8f\u65b9\u6cd5\u6709\u591a\u79cd\u3002<\/p>\n<p><strong>\u6309\u884c\u6392\u5e8f<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u8981\u6309\u884c\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\u3002\u53ef\u4ee5\u4f7f\u7528 java.util.stream.Stream \u5c06\u4e8c\u7ef4\u6570\u7ec4\u5c55\u5e73\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<pre>int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};\n\nArrays.stream(array)  \/\/ \u5c06\u4e8c\u7ef4\u6570\u7ec4\u5c55\u5e73\u4e3a\u6d41\n    .flatMapToInt(Arrays::stream)  \/\/ \u5c06\u6bcf\u884c\u5c55\u5e73\u4e3a IntStream\n    .sorted()  \/\/ \u6392\u5e8f\n    .forEach(System.out::println);  \/\/ \u6253\u5370\u6392\u5e8f\u540e\u7684\u5143\u7d20<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6309\u5217\u6392\u5e8f<\/strong><\/p>\n<p>\u8981\u6309\u5217\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u9700\u8981\u5c06\u6570\u7ec4\u8f6c\u7f6e\u3002\u8f6c\u7f6e\u662f\u6307\u5c06\u884c\u548c\u5217\u4e92\u6362\u3002\u8f6c\u7f6e\u540e\u7684\u6570\u7ec4\u53ef\u4ee5\u6309\u884c\u6392\u5e8f\uff0c\u7136\u540e\u6062\u590d\u4e3a\u539f\u59cb\u5f62\u72b6\uff1a<\/p>\n<pre>int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};\n\n\/\/ \u8f6c\u7f6e\u6570\u7ec4\nint[][] transposedArray = new int[array[0].length][array.length];\nfor (int i = 0; i &lt; array.length; i++) {\n    for (int j = 0; j &lt; array[0].length; j++) {\n        transposedArray[j][i] = array[i][j];\n    }\n}\n\n\/\/ \u6309\u884c\u5bf9\u8f6c\u7f6e\u540e\u7684\u6570\u7ec4\u6392\u5e8f\nArrays.sort(transposedArray, (a, b) -&gt; Integer.compare(a[0], b[0]));\n\n\/\/ \u6062\u590d\u539f\u59cb\u5f62\u72b6\nfor (int i = 0; i &lt; array.length; i++) {\n    for (int j = 0; j &lt; array[0].length; j++) {\n        array[i][j] = transposedArray[j][i];\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u81ea\u5b9a\u4e49\u6392\u5e8f<\/strong><\/p>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668\u5141\u8bb8\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u5bf9\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6309\u5143\u7d20\u7684\u548c\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<pre>class SumComparator implements Comparator&lt;int[]&gt; {\n    @Override\n    public int compare(int[] a, int[] b) {\n        return Integer.compare(Arrays.stream(a).sum(), Arrays.stream(b).sum());\n    }\n}\n\nArrays.sort(array, new SumComparator());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u6392\u5e8fjava\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>\u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f\u9700\u5c06\u6570\u7ec4\u5c55\u5e73\u4e3a\u4e00\u7ef4\u6570\u7ec4\u6216\u8f6c\u7f6e\u540e\u6309\u884c\u6392\u5e8f\uff1a\u6309\u884c\u6392\u5e8f\uff1a\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\u5bf9\u5c55\u5e73\u7684\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u6309\u5217\u6392\u5e8f\uff1a\u8f6c\u7f6e\u6570\u7ec4\u540e\u6309\u884c\u6392\u5e8f\uff0c\u518d\u6062\u590d\u4e3a\u539f\u59cb\u5f62\u72b6\u3002\u81ea\u5b9a\u4e49\u6392\u5e8f\uff1a\u4f7f\u7528\u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668\u6309\u7279\u5b9a\u6761\u4ef6\u5bf9\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\u3002 \u5982\u4f55\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f \u5165\u95e8 \u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u662f\u6307\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u5bf9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u91cd\u65b0\u3002\u53ef\u4ee5\u6839\u636e\u884c\u6216\u5217\u8fdb\u884c\u6392\u5e8f\uff0c\u6392\u5e8f\u65b9\u6cd5\u6709\u591a\u79cd\u3002 \u6309\u884c\u6392\u5e8f \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u8981\u6309\u884c\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\u3002\u53ef\u4ee5\u4f7f\u7528 java.util.stream.Stream \u5c06\u4e8c\u7ef4\u6570\u7ec4\u5c55\u5e73\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\uff1a int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Arrays.stream(array) \/\/ \u5c06\u4e8c\u7ef4\u6570\u7ec4\u5c55\u5e73\u4e3a\u6d41 .flatMapToInt(Arrays::stream) \/\/ \u5c06\u6bcf\u884c\u5c55\u5e73\u4e3a IntStream .sorted() \/\/ \u6392\u5e8f .forEach(System.out::println); \/\/ \u6253\u5370\u6392\u5e8f\u540e\u7684\u5143\u7d20 \u767b\u5f55\u540e\u590d\u5236 \u6309\u5217\u6392\u5e8f \u8981\u6309\u5217\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u9700\u8981\u5c06\u6570\u7ec4\u8f6c\u7f6e\u3002\u8f6c\u7f6e\u662f\u6307\u5c06\u884c\u548c\u5217\u4e92\u6362\u3002\u8f6c\u7f6e\u540e\u7684\u6570\u7ec4\u53ef\u4ee5\u6309\u884c\u6392\u5e8f\uff0c\u7136\u540e\u6062\u590d\u4e3a\u539f\u59cb\u5f62\u72b6\uff1a int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; \/\/ \u8f6c\u7f6e\u6570\u7ec4 [&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-66720","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66720","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=66720"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66720\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}