{"id":39763,"date":"2024-11-26T10:38:10","date_gmt":"2024-11-26T02:38:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39763\/"},"modified":"2024-11-26T10:38:10","modified_gmt":"2024-11-26T02:38:10","slug":"%e6%80%8e%e4%b9%88%e7%94%a8java%e6%95%b0%e7%bb%84%e4%bb%8e%e5%b0%8f%e5%88%b0%e5%a4%a7%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39763\/","title":{"rendered":"\u600e\u4e48\u7528java\u6570\u7ec4\u4ece\u5c0f\u5230\u5927\u6392\u5e8f"},"content":{"rendered":"<blockquote><p>\n  \u5982\u4f55\u4f7f\u7528 java \u6570\u7ec4\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff1fjava \u63d0\u4f9b\u4e86\u4ee5\u4e0b\u65b9\u6cd5\u5bf9\u6570\u7ec4\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff1aarrays.sort() \u65b9\u6cd5\uff1a\u7528\u4e8e\u5bf9\u57fa\u672c\u7c7b\u578b\u548c\u5bf9\u8c61\u6570\u7ec4\u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u6216\u5f52\u5e76\u6392\u5e8f\u3002\u81ea\u6211\u6392\u5e8f\uff08\u57fa\u672c\u7c7b\u578b\u6570\u7ec4\uff09\uff1a\u5192\u6ce1\u6392\u5e8f\u63d2\u5165\u6392\u5e8f\u9009\u62e9\u6392\u5e8f\u81ea\u6211\u6392\u5e8f\uff08\u5bf9\u8c61\u6570\u7ec4\uff09\uff1a\u9700\u8981\u5b9e\u73b0 comparable \u63a5\u53e3\u5e76\u63d0\u4f9b compareto() \u65b9\u6cd5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111709512685230.jpg\" class=\"aligncenter\" title=\"\u600e\u4e48\u7528java\u6570\u7ec4\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u63d2\u56fe\" alt=\"\u600e\u4e48\u7528java\u6570\u7ec4\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u6570\u7ec4\u4ece\u5c0f\u5230\u5927\u6392\u5e8f<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5bf9\u6570\u7ec4\u4ece\u5c0f\u5230\u5927\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<p><strong>1. Arrays.sort() \u65b9\u6cd5<\/strong><\/p>\n<ul>\n<li>Arrays.sort() \u65b9\u6cd5\u662f\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\uff0c\u7528\u4e8e\u5bf9\u57fa\u672c\u7c7b\u578b\u548c\u5bf9\u8c61\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<li>\u5b83\u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u6216\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\uff0c\u8fd9\u53d6\u51b3\u4e8e\u6570\u7ec4\u7684\u5927\u5c0f\u548c\u5143\u7d20\u7684\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] arr = {5, 2, 8, 3, 6};\nArrays.sort(arr);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u81ea\u6211\u6392\u5e8f<\/strong><\/p>\n<ul>\n<li>\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u81ea\u6211\u6392\u5e8f\u7b97\u6cd5\uff1a<\/li>\n<\/ul>\n<p><strong>\uff1a<\/strong><\/p>\n<pre>int[] arr = {5, 2, 8, 3, 6};\nfor (int i = 0; i &lt; arr.length - 1; i++) {\n    for (int j = 0; j &lt; arr.length - i - 1; j++) {\n        if (arr[j] &gt; arr[j + 1]) {\n            int temp = arr[j];\n            arr[j] = arr[j + 1];\n            arr[j + 1] = temp;\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u63d2\u5165\u6392\u5e8f\uff1a<\/strong><\/p>\n<pre>int[] arr = {5, 2, 8, 3, 6};\nfor (int i = 1; i &lt; arr.length; i++) {\n    int key = arr[i];\n    int j = i - 1;\n    while (j &gt;= 0 &amp;&amp; arr[j] &gt; key) {\n        arr[j + 1] = arr[j];\n        j--;\n    }\n    arr[j + 1] = key;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u6392\u5e8f\uff1a<\/strong><\/p>\n<pre>int[] arr = {5, 2, 8, 3, 6};\nfor (int i = 0; i &lt; arr.length - 1; i++) {\n    int minIndex = i;\n    for (int j = i + 1; j &lt; arr.length; j++) {\n        if (arr[j] &lt; arr[minIndex]) {\n            minIndex = j;\n        }\n    }\n    int temp = arr[i];\n    arr[i] = arr[minIndex];\n    arr[minIndex] = temp;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u81ea\u6211\u6392\u5e8f\uff08\u5bf9\u8c61\u6570\u7ec4\uff09<\/strong><\/p>\n<ul>\n<li>\u5bf9\u4e8e\u5bf9\u8c61\u6570\u7ec4\uff0c\u9700\u8981\u5b9e\u73b0 Comparable \u63a5\u53e3\u5e76\u63d0\u4f9b compareTo() \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>class Person implements Comparable&lt;Person&gt; {\n    private int age;\n    private String name;\n\n    @Override\n    public int compareTo(Person other) {\n        return this.age - other.age;\n    }\n}\n\nPerson[] people = {\n    new Person(20, \"Alice\"),\n    new Person(30, \"Bob\"),\n    new Person(15, \"Charlie\")\n};\nArrays.sort(people);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u7528java\u6570\u7ec4\u4ece\u5c0f\u5230\u5927\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>\u5982\u4f55\u4f7f\u7528 java \u6570\u7ec4\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff1fjava \u63d0\u4f9b\u4e86\u4ee5\u4e0b\u65b9\u6cd5\u5bf9\u6570\u7ec4\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff1aarrays.sort() \u65b9\u6cd5\uff1a\u7528\u4e8e\u5bf9\u57fa\u672c\u7c7b\u578b\u548c\u5bf9\u8c61\u6570\u7ec4\u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u6216\u5f52\u5e76\u6392\u5e8f\u3002\u81ea\u6211\u6392\u5e8f\uff08\u57fa\u672c\u7c7b\u578b\u6570\u7ec4\uff09\uff1a\u5192\u6ce1\u6392\u5e8f\u63d2\u5165\u6392\u5e8f\u9009\u62e9\u6392\u5e8f\u81ea\u6211\u6392\u5e8f\uff08\u5bf9\u8c61\u6570\u7ec4\uff09\uff1a\u9700\u8981\u5b9e\u73b0 comparable \u63a5\u53e3\u5e76\u63d0\u4f9b compareto() \u65b9\u6cd5\u3002 \u5982\u4f55\u4f7f\u7528 Java \u6570\u7ec4\u4ece\u5c0f\u5230\u5927\u6392\u5e8f \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5bf9\u6570\u7ec4\u4ece\u5c0f\u5230\u5927\u8fdb\u884c\u6392\u5e8f\uff1a 1. Arrays.sort() \u65b9\u6cd5 Arrays.sort() \u65b9\u6cd5\u662f\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\uff0c\u7528\u4e8e\u5bf9\u57fa\u672c\u7c7b\u578b\u548c\u5bf9\u8c61\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 \u5b83\u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u6216\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5\uff0c\u8fd9\u53d6\u51b3\u4e8e\u6570\u7ec4\u7684\u5927\u5c0f\u548c\u5143\u7d20\u7684\u7c7b\u578b\u3002 \u7528\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] arr = {5, 2, 8, 3, 6}; Arrays.sort(arr); \u767b\u5f55\u540e\u590d\u5236 2. \u81ea\u6211\u6392\u5e8f \u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u81ea\u6211\u6392\u5e8f\u7b97\u6cd5\uff1a \uff1a int[] arr = {5, 2, 8, 3, 6}; for (int i = 0; i &lt; arr.length &#8211; 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-39763","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39763","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=39763"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39763\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}