{"id":66085,"date":"2025-05-03T08:06:28","date_gmt":"2025-05-03T00:06:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66085\/"},"modified":"2025-05-03T08:06:28","modified_gmt":"2025-05-03T00:06:28","slug":"%e6%80%8e%e4%b9%88%e5%af%b9%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e6%8e%92%e5%ba%8fjava-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66085\/","title":{"rendered":"\u600e\u4e48\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8fjava"},"content":{"rendered":"<blockquote><p>\n  \u5982\u4f55\u5bf9 java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1a\u6309\u884c\u6392\u5e8f\uff1a\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\u5bf9\u6bcf\u4e00\u884c\u8fdb\u884c\u6392\u5e8f\u3002\u6309\u5217\u6392\u5e8f\uff1a\u8f6c\u7f6e\u6570\u7ec4\uff0c\u7136\u540e\u6309\u884c\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\uff0c\u518d\u5c06\u7ed3\u679c\u8f6c\u7f6e\u56de\u6765\u3002\u81ea\u5b9a\u4e49\u6392\u5e8f\uff1a\u5b9e\u73b0\u4e00\u4e2a comparator \u63a5\u53e3\uff0c\u5b9a\u4e49\u81ea\u5b9a\u4e49\u6bd4\u8f83\u903b\u8f91\uff0c\u7136\u540e\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111413491520759.jpg\" class=\"aligncenter\" title=\"\u600e\u4e48\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8fjava\u63d2\u56fe\" alt=\"\u600e\u4e48\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8fjava\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5bf9 Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f<\/strong><\/p>\n<p>\u4e8c\u7ef4\u6570\u7ec4\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u7531\u5b58\u50a8\u5728\u884c\u548c\u5217\u4e2d\u7684\u5143\u7d20\u7ec4\u6210\u3002\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u53ef\u4ee5\u5e2e\u52a9\u7ec4\u7ec7\u6570\u636e\u5e76\u4f7f\u5176\u66f4\u6613\u4e8e\u4f7f\u7528\u3002\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5bf9 Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p><strong>\u6309\u884c\u6392\u5e8f<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u6309\u884c\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u6b64\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u8981\u6392\u5e8f\u7684\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\u3002\u5bf9\u4e8e\u4e8c\u7ef4\u6570\u7ec4\uff0c\u9700\u8981\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u5bf9\u6bcf\u4e00\u884c\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[][] array = {\n    {1, 3, 2},\n    {5, 4, 6},\n    {7, 8, 9}\n};\n\nfor (int i = 0; i &lt; array.length; i++) {\n    Arrays.sort(array[i]);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>int[][] sortedArray = {\n    {1, 2, 3},\n    {4, 5, 6},\n    {7, 8, 9}\n};<\/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\u53ef\u4ee5\u901a\u8fc7\u8f6c\u7f6e\u6570\u7ec4\uff0c\u7136\u540e\u6309\u884c\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u3002\u8f6c\u7f6e\u6570\u7ec4\u610f\u5473\u7740\u4ea4\u6362\u884c\u548c\u5217\u3002<\/p>\n<pre>int[][] array = {\n    {1, 3, 2},\n    {5, 4, 6},\n    {7, 8, 9}\n};\n\nint[][] transposedArray = new int[array[0].length][array.length];\n\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\nfor (int i = 0; i &lt; transposedArray.length; i++) {\n    Arrays.sort(transposedArray[i]);\n}\n\nint[][] sortedArray = new int[array.length][array[0].length];\n\nfor (int i = 0; i &lt; array.length; i++) {\n    for (int j = 0; j &lt; array[0].length; j++) {\n        sortedArray[i][j] = transposedArray[j][i];\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>int[][] sortedArray = {\n    {1, 5, 7},\n    {2, 4, 8},\n    {3, 6, 9}\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u81ea\u5b9a\u4e49\u6392\u5e8f<\/strong><\/p>\n<p>\u8fd8\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668\u6309\u4efb\u610f\u6761\u4ef6\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668\u662f\u4e00\u4e2a\u5b9e\u73b0 Comparator \u63a5\u53e3\u7684\u7c7b\uff0c\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5143\u7d20\u3002<\/p>\n<pre>import java.util.Arrays;\nimport java.util.Comparator;\n\npublic class CustomComparator implements Comparator&lt;int[]&gt; {\n\n    @Override\n    public int compare(int[] o1, int[] o2) {\n        \/\/ \u81ea\u5b9a\u4e49\u6392\u5e8f\u903b\u8f91\n        \/\/ \u53ef\u4ee5\u6839\u636e\u5143\u7d20\u548c\u3001\u5e73\u5747\u503c\u3001\u6700\u5927\u503c\u6216\u4efb\u4f55\u5176\u4ed6\u81ea\u5b9a\u4e49\u6761\u4ef6\u8fdb\u884c\u6392\u5e8f\n        return 0;\n    }\n}\n\nint[][] array = {\n    {1, 3, 2},\n    {5, 4, 6},\n    {7, 8, 9}\n};\n\nArrays.sort(array, new CustomComparator());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668\uff0c\u53ef\u4ee5\u6839\u636e\u4efb\u4f55\u81ea\u5b9a\u4e49\u6761\u4ef6\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u5bf9\u4e8c\u7ef4\u6570\u7ec4\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>\u5982\u4f55\u5bf9 java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1a\u6309\u884c\u6392\u5e8f\uff1a\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\u5bf9\u6bcf\u4e00\u884c\u8fdb\u884c\u6392\u5e8f\u3002\u6309\u5217\u6392\u5e8f\uff1a\u8f6c\u7f6e\u6570\u7ec4\uff0c\u7136\u540e\u6309\u884c\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\uff0c\u518d\u5c06\u7ed3\u679c\u8f6c\u7f6e\u56de\u6765\u3002\u81ea\u5b9a\u4e49\u6392\u5e8f\uff1a\u5b9e\u73b0\u4e00\u4e2a comparator \u63a5\u53e3\uff0c\u5b9a\u4e49\u81ea\u5b9a\u4e49\u6bd4\u8f83\u903b\u8f91\uff0c\u7136\u540e\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 \u5982\u4f55\u5bf9 Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f \u4e8c\u7ef4\u6570\u7ec4\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u7531\u5b58\u50a8\u5728\u884c\u548c\u5217\u4e2d\u7684\u5143\u7d20\u7ec4\u6210\u3002\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u53ef\u4ee5\u5e2e\u52a9\u7ec4\u7ec7\u6570\u636e\u5e76\u4f7f\u5176\u66f4\u6613\u4e8e\u4f7f\u7528\u3002\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5bf9 Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 \u6309\u884c\u6392\u5e8f \u53ef\u4ee5\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u6309\u884c\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u6b64\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u8981\u6392\u5e8f\u7684\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\u3002\u5bf9\u4e8e\u4e8c\u7ef4\u6570\u7ec4\uff0c\u9700\u8981\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u5bf9\u6bcf\u4e00\u884c\u8fdb\u884c\u6392\u5e8f\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[][] array = { {1, 3, 2}, {5, 4, 6}, {7, 8, 9} }; for (int i = 0; i &lt; array.length; i++) { Arrays.sort(array[i]); } \u767b\u5f55\u540e\u590d\u5236 \u6392\u5e8f\u540e\u7684\u6570\u7ec4\u5982\u4e0b\u6240\u793a\uff1a int[][] sortedArray = { {1, [&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-66085","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66085","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=66085"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66085\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}