{"id":66276,"date":"2025-05-03T15:33:06","date_gmt":"2025-05-03T07:33:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66276\/"},"modified":"2025-05-03T15:33:06","modified_gmt":"2025-05-03T07:33:06","slug":"java%e5%b0%8f%e6%95%b0%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e6%af%94%e8%be%83%e5%a4%a7%e5%b0%8f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66276\/","title":{"rendered":"java\u5c0f\u6570\u6570\u7ec4\u600e\u4e48\u6bd4\u8f83\u5927\u5c0f"},"content":{"rendered":"<blockquote><p>\n  \u6bd4\u8f83\u5c0f\u6570\u6570\u7ec4\u5927\u5c0f\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\uff1b\u4f7f\u7528 comparator \u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668\uff1b\u4f7f\u7528\u53cc\u91cd\u5faa\u73af\u6bd4\u8f83\u6240\u6709\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111505544132675.jpg\" class=\"aligncenter\" title=\"java\u5c0f\u6570\u6570\u7ec4\u600e\u4e48\u6bd4\u8f83\u5927\u5c0f\u63d2\u56fe\" alt=\"java\u5c0f\u6570\u6570\u7ec4\u600e\u4e48\u6bd4\u8f83\u5927\u5c0f\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u5c0f\u6570\u6570\u7ec4\u6bd4\u8f83\u5927\u5c0f<\/strong><\/p>\n<p><strong>\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>Java\u4e2d\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u6bd4\u8f83\u5c0f\u6570\u6570\u7ec4\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 Arrays.sort() \u65b9\u6cd5<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u6b64\u65b9\u6cd5\u5c06\u6570\u7ec4\u6392\u5e8f\uff0c\u7136\u540e\u6bd4\u8f83\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u503c\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>double[] numbers = {1.2, 3.4, 5.6, 7.8, 9.0};\nArrays.sort(numbers);\n\ndouble first = numbers[0]; \/\/ \u6700\u5c0f\u7684\u6570\ndouble last = numbers[numbers.length - 1]; \/\/ \u6700\u5927\u7684\u6570\n\nif (last - first &gt; 0.0) {\n    System.out.println(\"\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u6570\u5927\u4e8e\u6700\u5c0f\u6570\");\n} else if (last - first == 0.0) {\n    System.out.println(\"\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u6570\u76f8\u7b49\");\n} else {\n    System.out.println(\"\u6570\u7ec4\u4e2d\u7684\u6700\u5c0f\u6570\u5927\u4e8e\u6700\u5927\u6570\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Comparator<\/strong><\/p>\n<p>\u81ea\u5b9a\u4e49\u4e00\u4e2a Comparator \u6765\u6bd4\u8f83\u5c0f\u6570\uff1a<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>import java.util.Arrays;\nimport java.util.Comparator;\n\npublic class DecimalComparator implements Comparator&lt;Double&gt; {\n    @Override\n    public int compare(Double o1, Double o2) {\n        return o1.compareTo(o2);\n    }\n}\n\npublic static void main(String[] args) {\n    double[] numbers = {1.2, 3.4, 5.6, 7.8, 9.0};\n\n    Arrays.sort(numbers, new DecimalComparator());\n\n    double first = numbers[0]; \/\/ \u6700\u5c0f\u7684\u6570\n    double last = numbers[numbers.length - 1]; \/\/ \u6700\u5927\u7684\u6570\n\n    if (last - first &gt; 0.0) {\n        System.out.println(\"\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u6570\u5927\u4e8e\u6700\u5c0f\u6570\");\n    } else if (last - first == 0.0) {\n        System.out.println(\"\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u6570\u76f8\u7b49\");\n    } else {\n        System.out.println(\"\u6570\u7ec4\u4e2d\u7684\u6700\u5c0f\u6570\u5927\u4e8e\u6700\u5927\u6570\");\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528\u53cc\u91cd\u5faa\u73af<\/strong><\/p>\n<p>\u4f7f\u7528\u53cc\u91cd\u5faa\u73af\u6bd4\u8f83\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff1a<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>double[] numbers = {1.2, 3.4, 5.6, 7.8, 9.0};\nboolean isMaxGreaterThanMin = false;\n\nfor (int i = 0; i &lt; numbers.length; i++) {\n    for (int j = i + 1; j &lt; numbers.length; j++) {\n        if (numbers[i] &lt; numbers[j]) {\n            isMaxGreaterThanMin = true;\n            break;\n        }\n    }\n    if (isMaxGreaterThanMin) {\n        break;\n    }\n}\n\nif (isMaxGreaterThanMin) {\n    System.out.println(\"\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u6570\u5927\u4e8e\u6700\u5c0f\u6570\");\n} else {\n    System.out.println(\"\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u6570\u76f8\u7b49\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u5c0f\u6570\u6570\u7ec4\u600e\u4e48\u6bd4\u8f83\u5927\u5c0f\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>\u6bd4\u8f83\u5c0f\u6570\u6570\u7ec4\u5927\u5c0f\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\uff1b\u4f7f\u7528 comparator \u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668\uff1b\u4f7f\u7528\u53cc\u91cd\u5faa\u73af\u6bd4\u8f83\u6240\u6709\u5143\u7d20\u3002 Java\u5c0f\u6570\u6570\u7ec4\u6bd4\u8f83\u5927\u5c0f \u65b9\u6cd5\uff1a Java\u4e2d\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u6bd4\u8f83\u5c0f\u6570\u6570\u7ec4\uff1a 1. \u4f7f\u7528 Arrays.sort() \u65b9\u6cd5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u6b64\u65b9\u6cd5\u5c06\u6570\u7ec4\u6392\u5e8f\uff0c\u7136\u540e\u6bd4\u8f83\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u503c\u3002 \u793a\u4f8b\uff1a double[] numbers = {1.2, 3.4, 5.6, 7.8, 9.0}; Arrays.sort(numbers); double first = numbers[0]; \/\/ \u6700\u5c0f\u7684\u6570 double last = numbers[numbers.length &#8211; 1]; \/\/ \u6700\u5927\u7684\u6570 if (last &#8211; first &gt; 0.0) { System.out.println(&#8220;\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u6570\u5927\u4e8e\u6700\u5c0f\u6570&#8221;); } else if (last &#8211; first == 0.0) { [&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-66276","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66276","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=66276"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66276\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}