{"id":202802,"date":"2025-05-22T09:50:54","date_gmt":"2025-05-22T01:50:54","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202802\/"},"modified":"2025-05-22T09:50:54","modified_gmt":"2025-05-22T01:50:54","slug":"%e6%8f%90%e5%8d%87-java-%e5%87%bd%e6%95%b0%e6%95%88%e7%8e%87%e7%9a%84%e6%8a%80%e5%b7%a7%e5%92%8c%e7%ad%96%e7%95%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202802\/","title":{"rendered":"\u63d0\u5347 Java \u51fd\u6570\u6548\u7387\u7684\u6280\u5de7\u548c\u7b56\u7565"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u63d0\u5347 Java \u51fd\u6570\u6548\u7387\u7684\u6280\u5de7\u548c\u7b56\u7565<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u63d0\u5347 Java \u51fd\u6570\u6548\u7387\u7684\u6280\u5de7\u548c\u7b56\u7565\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p>\u63d0\u5347 Java \u51fd\u6570\u6548\u7387\u7684\u6280\u5de7\uff1a\u907f\u514d\u9012\u5f52\uff0c\u4f7f\u7528\u8fed\u4ee3\u4ee3\u66ff\u3002\u4f7f\u7528\u9002\u5f53\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u54c8\u5e0c\u8868\u67e5\u627e\u5143\u7d20\u3002\u51cf\u5c11\u51fd\u6570\u590d\u6742\u5ea6\uff0c\u964d\u4f4e\u6700\u574f\u60c5\u51b5\u4e0b\u7684\u6027\u80fd\u3002\u5185\u8054\u51fd\u6570\u4ee5\u51cf\u5c11\u5f00\u9500\u3002\u5bf9\u4e8e\u957f\u65f6\u95f4\u8fd0\u884c\u4efb\u52a1\uff0c\u4f7f\u7528\u7ebf\u7a0b\u6c60\u4ee5\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729943241671cd6c998bd9.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u63d0\u5347 Java \u51fd\u6570\u6548\u7387\u7684\u6280\u5de7\u548c\u7b56\u7565<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u5728\u7f16\u5199 Java \u4ee3\u7801\u65f6\uff0c\u786e\u4fdd\u51fd\u6570\u9ad8\u6548\u81f3\u5173\u91cd\u8981\u3002\u9ad8\u6548\u7684\u51fd\u6570\u53ef\u4ee5\u51cf\u5c11\u5ef6\u8fdf\u3001\u63d0\u9ad8\u6027\u80fd\u5e76\u6539\u5584\u6574\u4f53\u5e94\u7528\u7a0b\u5e8f\u54cd\u5e94\u80fd\u529b\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u6280\u5de7\u548c\u7b56\u7565\uff0c\u5e2e\u52a9\u60a8\u63d0\u5347 Java \u51fd\u6570\u7684\u6548\u7387\u3002<\/p>\n<p><strong>1. \u907f\u514d\u9012\u5f52<\/strong><\/p>\n<p>\u9012\u5f52\u51fd\u6570\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6548\u7387\u4f4e\u4e0b\uff0c\u56e0\u4e3a\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6808\u5e27\u3002\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u8fed\u4ee3\u4ee3\u66ff\u9012\u5f52\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u9012\u5f52\uff1a<\/p>\n<pre>private int factorial(int n) {\n    if (n == 0) {\n        return 1;\n    } else {\n        return n * factorial(n - 1);\n    }\n}<\/pre>\n<p>\u8fed\u4ee3\uff1a<\/p>\n<pre>private int factorial(int n) {\n    int result = 1;\n    for (int i = 1; i &lt;= n; i++) {\n        result *= i;\n    }\n    return result;\n}<\/pre>\n<p><strong>2. \u4f7f\u7528\u9002\u5f53\u7684\u6570\u636e\u7ed3\u6784<\/strong><\/p>\n<p>\u9009\u62e9\u6b63\u786e\u7684\u6570\u636e\u7ed3\u6784\u5bf9\u4e8e\u51fd\u6570\u6548\u7387\u81f3\u5173\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u67e5\u627e\u5143\u7d20\u65f6\uff0c\u4f7f\u7528\u54c8\u5e0c\u8868\u6bd4\u4f7f\u7528\u6570\u7ec4\u6216\u94fe\u8868\u66f4\u6709\u6548\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u67e5\u627e\u5143\u7d20\uff1a<\/p>\n<pre>\/\/ \u4f7f\u7528\u6570\u7ec4\uff08\u6548\u7387\u4f4e\uff09\nint index = -1;\nfor (int i = 0; i &lt; array.length; i++) {\n    if (array[i] == element) {\n        index = i;\n        break;\n    }\n}\n\n\/\/ \u4f7f\u7528\u54c8\u5e0c\u8868\uff08\u6548\u7387\u9ad8\uff09\nHashMap&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();\nif (map.containsKey(element)) {\n    index = map.get(element);\n}<\/pre>\n<p><strong>3. \u51cf\u5c11\u51fd\u6570\u590d\u6742\u5ea6<\/strong><\/p>\n<p>\u51fd\u6570\u7684\u590d\u6742\u5ea6\u8861\u91cf\u5176\u5728\u6700\u574f\u60c5\u51b5\u4e0b\u7684\u6027\u80fd\u3002\u590d\u6742\u5ea6\u8d8a\u4f4e\uff0c\u51fd\u6570\u8d8a\u6709\u6548\u7387\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u590d\u6742\u5ea6 O(n)\uff1a<\/p>\n<pre>private void findMax(int[] arr) {\n    int max = Integer.MIN_VALUE;\n    for (int i = 0; i &lt; arr.length; i++) {\n        if (arr[i] &gt; max) {\n            max = arr[i];\n        }\n    }\n}<\/pre>\n<p>\u590d\u6742\u5ea6 O(1)\uff1a<\/p>\n<pre>private int findMax(int[] arr) {\n    return Arrays.stream(arr).max().getAsInt();\n}<\/pre>\n<p><strong>4. \u5185\u8054\u51fd\u6570<\/strong><\/p>\n<p>\u5185\u8054\u51fd\u6570\u5c06\u51fd\u6570\u8c03\u7528\u66ff\u6362\u4e3a\u51fd\u6570\u4f53\u672c\u8eab\u3002\u8fd9\u53ef\u4ee5\u51cf\u5c11\u5f00\u9500\u5e76\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u539f\u59cb\uff1a<\/p>\n<pre>private void printMessage() {\n    System.out.println(\"Hello World!\");\n}<\/pre>\n<p>\u5185\u8054\uff1a<\/p>\n<pre>private void printMessage() {\n    System.out.println(helloMessage);\n}<\/pre>\n<p><strong>5. \u4f7f\u7528\u7ebf\u7a0b\u6c60<\/strong><\/p>\n<p>\u5bf9\u4e8e\u6d89\u53ca\u957f\u65f6\u95f4\u8fd0\u884c\u4efb\u52a1\u7684\u51fd\u6570\uff0c\u4f7f\u7528\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\u3002\u7ebf\u7a0b\u6c60\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\uff0c\u907f\u514d\u4e86\u521b\u5efa\u65b0\u7ebf\u7a0b\u7684\u5f00\u9500\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>ExecutorService executor = Executors.newFixedThreadPool(10);\nfor (int i = 0; i &lt; 100; i++) {\n    executor.submit(() -&gt; {\n        \/\/ \u6267\u884c\u4efb\u52a1\n    });\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b Java \u51fd\u6570\uff0c\u7528\u4e8e\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u5143\u7d20\uff1a<\/p>\n<pre>private int findMax(int[] arr) {\n    int max = Integer.MIN_VALUE;\n    for (int i = 0; i &lt; arr.length; i++) {\n        if (arr[i] &gt; max) {\n            max = arr[i];\n        }\n    }\n    return max;\n}<\/pre>\n<p>\u5e94\u7528\u4ee5\u4e0a\u7b56\u7565\uff0c\u53ef\u4ee5\u63d0\u9ad8\u8be5\u51fd\u6570\u7684\u6548\u7387\uff1a<\/p>\n<ul>\n<li>\u5c06\u5176\u4e0e Arrays.stream().max() \u5185\u8054\uff1a<\/li>\n<\/ul>\n<pre>private int findMax(int[] arr) {\n    return Arrays.stream(arr).max().getAsInt();\n}<\/pre>\n<ul>\n<li>\u590d\u6742\u5ea6\u73b0\u5728\u4e3a O(1)\uff0c\u56e0\u4e3a Arrays.stream().max() \u4f7f\u7528\u5e76\u884c\u6d41\u9ad8\u6548\u5730\u67e5\u627e\u6700\u5927\u503c\u3002<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u63d0\u5347 Java \u51fd\u6570\u6548\u7387\u7684\u6280\u5de7\u548c\u7b56\u7565\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63d0\u5347 Java \u51fd\u6570\u6548\u7387\u7684\u6280\u5de7\u548c&#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-202802","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202802","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=202802"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202802\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}