{"id":65575,"date":"2025-05-03T15:31:34","date_gmt":"2025-05-03T07:31:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65575\/"},"modified":"2025-05-03T15:31:34","modified_gmt":"2025-05-03T07:31:34","slug":"java%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e8%be%93%e5%87%ba%e6%ac%a1%e5%a4%a7%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65575\/","title":{"rendered":"java\u6570\u7ec4\u600e\u4e48\u8f93\u51fa\u6b21\u5927\u503c"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u8f93\u51fa\u6570\u7ec4\u4e2d\u7684\u6b21\u5927\u503c\u7684\u65b9\u6cd5\u4e3a\uff1a\u6392\u5e8f\u6570\u7ec4\u3002\u68c0\u67e5\u7279\u6b8a\u60c5\u51b5\uff08\u6570\u7ec4\u4e3a\u7a7a\u6216\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff09\u3002\u67e5\u627e\u6b21\u5927\u503c\uff08\u4f4d\u4e8e\u6392\u5e8f\u540e\u6570\u7ec4\u7684\u5012\u6570\u7b2c\u4e8c\u4e2a\u4f4d\u7f6e\uff09\u3002\u8fd4\u56de\u6b21\u5927\u503c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111304152980036.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u600e\u4e48\u8f93\u51fa\u6b21\u5927\u503c\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u600e\u4e48\u8f93\u51fa\u6b21\u5927\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u8f93\u51fa Java \u6570\u7ec4\u4e2d\u7684\u6b21\u5927\u503c<\/strong><\/p>\n<p>\u5728 Java \u4e2d\u8f93\u51fa\u6570\u7ec4\u4e2d\u7684\u6b21\u5927\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u6392\u5e8f\u6570\u7ec4<\/strong><\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5c06\u6570\u7ec4\u6392\u5e8f\uff0c\u4ee5\u4fbf\u6839\u636e\u5143\u7d20\u5927\u5c0f\u3002\u53ef\u4ee5\u4f7f\u7528 [java.util.Arrays.sort()](https:\/\/docs..com\/javase\/7\/docs\/api\/java\/util\/Arrays.html) \u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<pre>Arrays.sort(array);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u68c0\u67e5\u7279\u6b8a\u60c5\u51b5<\/strong><\/li>\n<\/ol>\n<p>\u5982\u679c\u6570\u7ec4\u4e3a\u7a7a\u6216\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff0c\u5219\u6ca1\u6709\u6b21\u5927\u503c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8fd4\u56de\u7279\u5b9a\u7684\u503c\u6216\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>if (array.length &lt; 2) {\n    throw new IllegalArgumentException(\"\u6570\u7ec4\u957f\u5ea6\u5fc5\u987b\u5927\u4e8e 1\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u67e5\u627e\u6b21\u5927\u503c<\/strong><\/li>\n<\/ol>\n<p>\u4ece\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u4e2d\uff0c\u6b21\u5927\u503c\u4f4d\u4e8e\u5012\u6570\u7b2c\u4e8c\u4e2a\u4f4d\u7f6e\u3002<\/p>\n<pre>int secondLargest = array[array.length - 2];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u8fd4\u56de\u6b21\u5927\u503c<\/strong><\/li>\n<\/ol>\n<p>\u6700\u540e\uff0c\u5c06\u6b21\u5927\u503c\u8fd4\u56de\u3002<\/p>\n<pre>return secondLargest;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] array = {10, 5, 20, 12, 15};\n\nArrays.sort(array);\n\nif (array.length &lt; 2) {\n    throw new IllegalArgumentException(\"\u6570\u7ec4\u957f\u5ea6\u5fc5\u987b\u5927\u4e8e 1\");\n}\n\nint secondLargest = array[array.length - 2];\n\nSystem.out.println(secondLargest); \/\/ \u8f93\u51fa\uff1a15<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u600e\u4e48\u8f93\u51fa\u6b21\u5927\u503c\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>\u5728 java \u4e2d\uff0c\u8f93\u51fa\u6570\u7ec4\u4e2d\u7684\u6b21\u5927\u503c\u7684\u65b9\u6cd5\u4e3a\uff1a\u6392\u5e8f\u6570\u7ec4\u3002\u68c0\u67e5\u7279\u6b8a\u60c5\u51b5\uff08\u6570\u7ec4\u4e3a\u7a7a\u6216\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff09\u3002\u67e5\u627e\u6b21\u5927\u503c\uff08\u4f4d\u4e8e\u6392\u5e8f\u540e\u6570\u7ec4\u7684\u5012\u6570\u7b2c\u4e8c\u4e2a\u4f4d\u7f6e\uff09\u3002\u8fd4\u56de\u6b21\u5927\u503c\u3002 \u5982\u4f55\u8f93\u51fa Java \u6570\u7ec4\u4e2d\u7684\u6b21\u5927\u503c \u5728 Java \u4e2d\u8f93\u51fa\u6570\u7ec4\u4e2d\u7684\u6b21\u5927\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u6392\u5e8f\u6570\u7ec4 \u9996\u5148\uff0c\u9700\u8981\u5c06\u6570\u7ec4\u6392\u5e8f\uff0c\u4ee5\u4fbf\u6839\u636e\u5143\u7d20\u5927\u5c0f\u3002\u53ef\u4ee5\u4f7f\u7528 [java.util.Arrays.sort()](https:\/\/docs..com\/javase\/7\/docs\/api\/java\/util\/Arrays.html) \u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f\u3002 Arrays.sort(array); \u767b\u5f55\u540e\u590d\u5236 \u68c0\u67e5\u7279\u6b8a\u60c5\u51b5 \u5982\u679c\u6570\u7ec4\u4e3a\u7a7a\u6216\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff0c\u5219\u6ca1\u6709\u6b21\u5927\u503c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8fd4\u56de\u7279\u5b9a\u7684\u503c\u6216\u629b\u51fa\u5f02\u5e38\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b if (array.length &lt; 2) { throw new IllegalArgumentException(&#8220;\u6570\u7ec4\u957f\u5ea6\u5fc5\u987b\u5927\u4e8e 1&#8221;); } \u767b\u5f55\u540e\u590d\u5236 \u67e5\u627e\u6b21\u5927\u503c \u4ece\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u4e2d\uff0c\u6b21\u5927\u503c\u4f4d\u4e8e\u5012\u6570\u7b2c\u4e8c\u4e2a\u4f4d\u7f6e\u3002 int secondLargest = array[array.length &#8211; 2]; \u767b\u5f55\u540e\u590d\u5236 \u8fd4\u56de\u6b21\u5927\u503c \u6700\u540e\uff0c\u5c06\u6b21\u5927\u503c\u8fd4\u56de\u3002 return secondLargest; \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a int[] array = {10, 5, 20, 12, 15}; Arrays.sort(array); if (array.length [&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-65575","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65575","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=65575"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65575\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}