{"id":38347,"date":"2024-11-26T10:05:06","date_gmt":"2024-11-26T02:05:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38347\/"},"modified":"2024-11-26T10:05:06","modified_gmt":"2024-11-26T02:05:06","slug":"java%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%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38347\/","title":{"rendered":"java\u600e\u4e48\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a\u4f7f\u7528 arrays.sort() \u5c06\u6bcf\u4e00\u884c\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u5bf9\u8fd9\u4e9b\u4e00\u7ef4\u6570\u7ec4\u5206\u522b\u8c03\u7528 arrays.sort() \u65b9\u6cd5\uff1b\u4f7f\u7528 comparator \u548c arrays.sort() \u6765\u5b9a\u4e49\u81ea\u5b9a\u4e49\u6bd4\u8f83\u89c4\u5219\uff0c\u7136\u540e\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\u6309\u7167\u7279\u5b9a\u5217\u6216\u5143\u7d20\u6392\u5e8f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111413274263557.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f<\/strong><\/p>\n<p><strong>\u5982\u4f55\u5bf9 Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><\/p>\n<p>Java \u4e2d\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 Arrays.sort()<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>Arrays.sort() \u65b9\u6cd5\u53ef\u4ee5\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u6bcf\u4e00\u884c\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u5bf9\u8fd9\u4e9b\u4e00\u7ef4\u6570\u7ec4\u5206\u522b\u8c03\u7528 Arrays.sort() \u65b9\u6cd5\u6765\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f\u3002<\/p>\n<pre>int[][] array = {\n    {1, 3, 2},\n    {5, 4, 6},\n    {7, 8, 9}\n};\n\n\/\/ \u904d\u5386\u6bcf\u4e00\u884c\uff0c\u5e76\u5bf9\u6bcf\u4e00\u884c\u8c03\u7528 Arrays.sort()\nfor (int[] row : array) {\n    Arrays.sort(row);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Comparator \u548c Arrays.sort()<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Comparator \u6765\u5b9a\u4e49\u81ea\u5b9a\u4e49\u6bd4\u8f83\u89c4\u5219\uff0c\u7136\u540e\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u8fd9\u901a\u5e38\u7528\u4e8e\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u7684\u7279\u5b9a\u5217\u6216\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<pre>\/\/ \u5b9a\u4e49\u4e00\u4e2a\u6bd4\u8f83\u5668\u6765\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u7684\u7b2c\u4e8c\u5217\u6392\u5e8f\nComparator&lt;int[]&gt; comparator = (a, b) -&gt; a[1] - b[1];\n\n\/\/ \u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\uff0c\u5e76\u4f20\u5165\u6bd4\u8f83\u5668\nArrays.sort(array, comparator);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a<\/strong><\/p>\n<ul>\n<li>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u4f1a\u4fee\u6539\u539f\u59cb\u4e8c\u7ef4\u6570\u7ec4\u3002<\/li>\n<li>\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u4f8b\u5982 Arrays.sort(row, Collections.reverseOrder()) \u6765\u6309\u964d\u5e8f\u5bf9\u6bcf\u4e00\u884c\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<li>\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0c\u4f7f\u7528\u57fa\u4e8e\u6811\u7684\u6392\u5e8f\u7b97\u6cd5\uff08\u5982\u5f52\u5e76\u6392\u5e8f\u6216\u5806\u6392\u5e8f\uff09\u53ef\u80fd\u6bd4 Arrays.sort() \u6548\u7387\u66f4\u9ad8\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5bf9\u4e8c\u7ef4\u6570\u7ec4\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\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a\u4f7f\u7528 arrays.sort() \u5c06\u6bcf\u4e00\u884c\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u5bf9\u8fd9\u4e9b\u4e00\u7ef4\u6570\u7ec4\u5206\u522b\u8c03\u7528 arrays.sort() \u65b9\u6cd5\uff1b\u4f7f\u7528 comparator \u548c arrays.sort() \u6765\u5b9a\u4e49\u81ea\u5b9a\u4e49\u6bd4\u8f83\u89c4\u5219\uff0c\u7136\u540e\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\u6309\u7167\u7279\u5b9a\u5217\u6216\u5143\u7d20\u6392\u5e8f\u3002 Java \u4e2d\u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f \u5982\u4f55\u5bf9 Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1f Java \u4e2d\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 Arrays.sort() \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b Arrays.sort() \u65b9\u6cd5\u53ef\u4ee5\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u6bcf\u4e00\u884c\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u5bf9\u8fd9\u4e9b\u4e00\u7ef4\u6570\u7ec4\u5206\u522b\u8c03\u7528 Arrays.sort() \u65b9\u6cd5\u6765\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f\u3002 int[][] array = { {1, 3, 2}, {5, 4, 6}, {7, 8, 9} }; \/\/ \u904d\u5386\u6bcf\u4e00\u884c\uff0c\u5e76\u5bf9\u6bcf\u4e00\u884c\u8c03\u7528 Arrays.sort() for (int[] row : array) { Arrays.sort(row); } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 [&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-38347","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38347","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=38347"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38347\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}