{"id":202578,"date":"2025-05-22T10:21:11","date_gmt":"2025-05-22T02:21:11","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202578\/"},"modified":"2025-05-22T10:21:11","modified_gmt":"2025-05-22T02:21:11","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87%e6%a8%a1%e6%9d%bf%e6%96%b9%e6%b3%95%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e6%8f%90%e5%8d%87java%e5%87%bd%e6%95%b0%e5%8f%af%e5%a4%8d%e7%94%a8%e6%80%a7%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202578\/","title":{"rendered":"\u5982\u4f55\u901a\u8fc7\u6a21\u677f\u65b9\u6cd5\u8bbe\u8ba1\u6a21\u5f0f\u63d0\u5347Java\u51fd\u6570\u53ef\u590d\u7528\u6027\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u901a\u8fc7\u6a21\u677f\u65b9\u6cd5\u8bbe\u8ba1\u6a21\u5f0f\u63d0\u5347Java\u51fd\u6570\u53ef\u590d\u7528\u6027\uff1f<\/h1>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u901a\u8fc7\u6a21\u677f\u65b9\u6cd5\u8bbe\u8ba1\u6a21\u5f0f\u63d0\u5347Java\u51fd\u6570\u53ef\u590d\u7528\u6027\uff1f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p>\u6a21\u677f\u65b9\u6cd5\u8bbe\u8ba1\u6a21\u5f0f\u63d0\u5347\u4e86 Java \u51fd\u6570\u7684\u53ef\u590d\u7528\u6027\uff0c\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\u6765\u5b9a\u4e49\u516c\u5171\u903b\u8f91\uff0c\u5e76\u5141\u8bb8\u5177\u4f53\u5b50\u7c7b\u5b9a\u5236\u7279\u5b9a\u884c\u4e3a\u3002\u5b83\u5305\u62ec\uff1a\u62bd\u8c61\u57fa\u7c7b\u5b9a\u4e49\u516c\u5171\u903b\u8f91\u548c\u6a21\u677f\u65b9\u6cd5\u3002\u5177\u4f53\u5b50\u7c7b\u7ee7\u627f\u62bd\u8c61\u57fa\u7c7b\u5e76\u5b9e\u73b0\u6a21\u677f\u65b9\u6cd5\u4e2d\u7684\u53ef\u53d8\u90e8\u5206\u3002\u4f18\u70b9\u5305\u62ec\u63d0\u5347\u4ee3\u7801\u53ef\u590d\u7528\u6027\u3001\u63d0\u9ad8\u53ef\u6269\u5c55\u6027\u4ee5\u53ca\u6539\u5584\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729919487671c79ff0a09a.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u6a21\u677f\u65b9\u6cd5\u8bbe\u8ba1\u6a21\u5f0f\uff1a\u63d0\u5347 Java \u51fd\u6570\u53ef\u590d\u7528\u6027<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u6a21\u677f\u65b9\u6cd5\u8bbe\u8ba1\u6a21\u5f0f\u662f\u4e00\u79cd\u884c\u4e3a\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u5141\u8bb8\u5728\u4e0d\u540c\u51fd\u6570\u95f4\u5171\u4eab\u516c\u5171\u903b\u8f91\uff0c\u540c\u65f6\u4e3a\u5b50\u7c7b\u5b9a\u5236\u7279\u5b9a\u884c\u4e3a\u63d0\u4f9b\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>\u6a21\u5f0f\u7ed3\u6784<\/strong><\/p>\n<p>\u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f\u7531\u4ee5\u4e0b\u7ec4\u6210\uff1a<\/p>\n<ul>\n<li><strong>\u62bd\u8c61\u57fa\u7c7b:<\/strong> \u5b9a\u4e49\u516c\u5171\u903b\u8f91\u548c\u6a21\u677f\u65b9\u6cd5\u3002<\/li>\n<li><strong>\u5177\u4f53\u5b50\u7c7b:<\/strong> \u7ee7\u627f\u62bd\u8c61\u57fa\u7c7b\u5e76\u5b9e\u73b0\u6a21\u677f\u65b9\u6cd5\u4e2d\u7684\u53ef\u53d8\u90e8\u5206\u3002<\/li>\n<\/ul>\n<p><strong>\u6838\u5fc3\u539f\u7406<\/strong><\/p>\n<p>\u6a21\u677f\u65b9\u6cd5\u8bbe\u8ba1\u6a21\u5f0f\u7684\u6838\u5fc3\u539f\u7406\u662f\u5c06\u516c\u5171\u903b\u8f91\u62bd\u53d6\u5230\u62bd\u8c61\u57fa\u7c7b\u4e2d\uff0c\u5e76\u5728\u6a21\u677f\u65b9\u6cd5\u4e2d\u5b9a\u4e49\u7b97\u6cd5\u7684\u9aa8\u67b6\u3002\u5177\u4f53\u5b50\u7c7b\u901a\u8fc7\u8986\u5199\u6a21\u677f\u65b9\u6cd5\u4e2d\u7684\u53ef\u53d8\u90e8\u5206\u6765\u5b9a\u5236\u7279\u5b9a\u884c\u4e3a\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u6392\u5e8f\u51fd\u6570\u7684\u793a\u4f8b\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u5bf9\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u6392\u5e8f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u65b9\u6cd5\u8bbe\u8ba1\u6a21\u5f0f\u6765\u521b\u5efa\u6392\u5e8f\u51fd\u6570\u7684\u62bd\u8c61\u57fa\u7c7b\u548c\u5404\u79cd\u5177\u4f53\u5b50\u7c7b\uff0c\u9488\u5bf9\u6bcf\u79cd\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u5b9a\u5236\u6392\u5e8f\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<p><strong>\u62bd\u8c61\u57fa\u7c7b (Sorter):<\/strong><\/p>\n<pre>public abstract class Sorter {\n\n    protected void sort(Comparable[] elements) {\n        \/\/ \u516c\u5171\u6392\u5e8f\u903b\u8f91\n\n        for (int i = 1; i &lt; elements.length; i++) {\n            Comparable current = elements[i];\n            int j = i - 1;\n            while (j &gt;= 0 &amp;&amp; current.compareTo(elements[j]) &lt; 0) {\n                \/\/ \u5143\u7d20\u4ea4\u6362\u903b\u8f91\n                swap(elements, j, j + 1);\n                j--;\n            }\n        }\n    }\n\n    protected abstract boolean compare(Comparable a, Comparable b);\n\n    protected void swap(Comparable[] elements, int i, int j) {\n        Comparable temp = elements[i];\n        elements[i] = elements[j];\n        elements[j] = temp;\n    }\n\n}<\/pre>\n<p><strong>\u5177\u4f53\u5b50\u7c7b (IntegerSorter):<\/strong><\/p>\n<pre>public class IntegerSorter extends Sorter {\n\n    @Override\n    protected boolean compare(Comparable a, Comparable b) {\n        return (int)a &lt; (int)b;\n    }\n\n}<\/pre>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<pre>Comparable[] integers = {5, 3, 7, 2, 1, 9};\nSorter sorter = new IntegerSorter();\nsorter.sort(integers);\n\nfor (Comparable integer : integers) {\n    System.out.print(integer + \" \");\n}<\/pre>\n<p><strong>\u8f93\u51fa<\/strong><\/p>\n<pre>1 2 3 5 7 9<\/pre>\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n<ul>\n<li>\u63d0\u5347\u4ee3\u7801\u53ef\u590d\u7528\u6027\uff0c\u901a\u8fc7\u62bd\u8c61\u516c\u5171\u903b\u8f91\u548c\u5b9a\u5236\u53ef\u53d8\u90e8\u5206\u6765\u907f\u514d\u91cd\u590d\u4ee3\u7801\u3002<\/li>\n<li>\u63d0\u9ad8\u53ef\u6269\u5c55\u6027\uff0c\u5141\u8bb8\u5728\u4e0d\u5f71\u54cd\u73b0\u6709\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u6dfb\u52a0\u65b0\u529f\u80fd\uff08\u4f8b\u5982\uff0c\u6392\u5e8f\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff09\u3002<\/li>\n<li>\u6539\u5584\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\uff0c\u901a\u8fc7\u5c06\u5173\u6ce8\u70b9\u96c6\u4e2d\u5230\u53ef\u53d8\u90e8\u5206\u6765\u7b80\u5316\u4ee3\u7801\u7ed3\u6784\u3002<\/li>\n<\/ul>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u901a\u8fc7\u6a21\u677f\u65b9\u6cd5\u8bbe\u8ba1\u6a21\u5f0f\u63d0\u5347Java\u51fd\u6570\u53ef\u590d\u7528\u6027\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8ejava,\u6a21\u677f\u65b9\u6cd5\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u901a\u8fc7\u6a21\u677f\u65b9\u6cd5\u8bbe\u8ba1\u6a21\u5f0f\u63d0\u5347Ja&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202578","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202578"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202578\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}