{"id":40295,"date":"2024-11-26T17:57:07","date_gmt":"2024-11-26T09:57:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40295\/"},"modified":"2024-11-26T17:57:07","modified_gmt":"2024-11-26T09:57:07","slug":"java%e6%80%8e%e4%b9%88%e6%b1%82%e6%95%b0%e7%bb%84%e7%9a%84%e6%9c%80%e5%a4%a7%e5%80%bc%e5%92%8c%e6%9c%80%e5%b0%8f%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/40295\/","title":{"rendered":"java\u600e\u4e48\u6c42\u6570\u7ec4\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u6c42\u6570\u7ec4\u6700\u5927\u6700\u5c0f\u503c\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528 arrays.sort()\uff0c\u5bf9\u6570\u7ec4\u6392\u5e8f\u540e\u53d6\u9996\u5c3e\u5143\u7d20\u3002\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u8bb0\u5f55\u904d\u5386\u4e2d\u9047\u5230\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002\u4f7f\u7528 stream api\uff0c\u5206\u522b\u8c03\u7528 min() \u548c max() \u65b9\u6cd5\u53d6\u5f97\u7ed3\u679c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/18\/2024111816251279129.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u6c42\u6570\u7ec4\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u63d2\u56fe\" alt=\"java\u600e\u4e48\u6c42\u6570\u7ec4\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u6c42\u6570\u7ec4\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6c42\u6570\u7ec4\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002<\/p>\n<p><strong>\u4f7f\u7528 Arrays.sort()<\/strong><\/p>\n<p>Arrays.sort() \u65b9\u6cd5\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\u3002\u6392\u5e8f\u540e\uff0c\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u6700\u5c0f\u503c\uff0c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u662f\u6700\u5927\u503c\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] numbers = { 5, 2, 8, 3, 10, 1 };\n\nArrays.sort(numbers);\n\nint minValue = numbers[0];\nint maxValue = numbers[numbers.length - 1];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u5faa\u73af<\/strong><\/p>\n<p>\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u8ddf\u8e2a\u627e\u5230\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002<\/p>\n<pre>int[] numbers = { 5, 2, 8, 3, 10, 1 };\n\nint minValue = Integer.MAX_VALUE;\nint maxValue = Integer.MIN_VALUE;\n\nfor (int number : numbers) {\n    if (number &lt; minValue) {\n        minValue = number;\n    }\n    if (number &gt; maxValue) {\n        maxValue = number;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Stream API<\/strong><\/p>\n<p>Java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u652f\u6301 Stream API\uff0c\u53ef\u4ee5\u4f7f\u7528\u8be5 API \u7b80\u5316\u4ee3\u7801\u3002<\/p>\n<pre>int[] numbers = { 5, 2, 8, 3, 10, 1 };\n\nint minValue = Arrays.stream(numbers).min().getAsInt();\nint maxValue = Arrays.stream(numbers).max().getAsInt();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6027\u80fd\u8003\u8651<\/strong><\/p>\n<p>\u5bf9\u4e8e\u8f83\u5c0f\u7684\u6570\u7ec4\uff0c\u4efb\u4f55\u65b9\u6cd5\u90fd\u80fd\u63d0\u4f9b\u5408\u7406\u7684\u6027\u80fd\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u5927\u578b\u6570\u7ec4\uff0c\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u53ef\u80fd\u4f1a\u6bd4\u8f83\u6162\uff0c\u56e0\u4e3a\u8be5\u65b9\u6cd5\u9700\u8981\u6267\u884c\u5b8c\u6574\u7684\u6392\u5e8f\u64cd\u4f5c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\u7684\u65b9\u6cd5\u53ef\u80fd\u4f1a\u66f4\u6709\u6548\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u6c42\u6570\u7ec4\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\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\u6c42\u6570\u7ec4\u6700\u5927\u6700\u5c0f\u503c\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528 arrays.sort()\uff0c\u5bf9\u6570\u7ec4\u6392\u5e8f\u540e\u53d6\u9996\u5c3e\u5143\u7d20\u3002\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u8bb0\u5f55\u904d\u5386\u4e2d\u9047\u5230\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002\u4f7f\u7528 stream api\uff0c\u5206\u522b\u8c03\u7528 min() \u548c max() \u65b9\u6cd5\u53d6\u5f97\u7ed3\u679c\u3002 \u5982\u4f55\u5728 Java \u4e2d\u6c42\u6570\u7ec4\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c \u5728 Java \u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6c42\u6570\u7ec4\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002 \u4f7f\u7528 Arrays.sort() Arrays.sort() \u65b9\u6cd5\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\u3002\u6392\u5e8f\u540e\uff0c\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u6700\u5c0f\u503c\uff0c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u662f\u6700\u5927\u503c\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] numbers = { 5, 2, 8, 3, 10, 1 }; Arrays.sort(numbers); int minValue = numbers[0]; int maxValue = numbers[numbers.length &#8211; 1]; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528\u5faa\u73af \u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u8ddf\u8e2a\u627e\u5230\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002 int[] numbers = { 5, 2, 8, 3, 10, 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-40295","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40295","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=40295"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40295\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}