{"id":203074,"date":"2025-05-22T11:34:17","date_gmt":"2025-05-22T03:34:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203074\/"},"modified":"2025-05-22T11:34:17","modified_gmt":"2025-05-22T03:34:17","slug":"%e5%a6%82%e4%bd%95%e4%bc%98%e5%8c%96-java-%e5%87%bd%e6%95%b0%e5%92%8c%e6%96%b9%e6%b3%95%e7%9a%84%e6%80%a7%e8%83%bd%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203074\/","title":{"rendered":"\u5982\u4f55\u4f18\u5316 Java \u51fd\u6570\u548c\u65b9\u6cd5\u7684\u6027\u80fd\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f18\u5316 Java \u51fd\u6570\u548c\u65b9\u6cd5\u7684\u6027\u80fd\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5982\u4f55\u4f18\u5316 Java \u51fd\u6570\u548c\u65b9\u6cd5\u7684\u6027\u80fd\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p>Java \u51fd\u6570\u548c\u65b9\u6cd5\u7684\u6027\u80fd\u4f18\u5316\u6280\u5de7\u5305\u62ec\uff1a\u4f7f\u7528\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3001\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3001\u4f7f\u7528 StringBuilder\u3001\u4f18\u5316\u5faa\u73af\u3001\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u3002\u5b9e\u6218\u4e2d\uff0c\u901a\u8fc7\u91c7\u7528\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u4f18\u5316\u67e5\u627e\uff0c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4ece O(n) \u964d\u4f4e\u81f3 O(log n)\uff0c\u5927\u5e45\u63d0\u5347\u6548\u7387\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730039255671e4dd77c34d.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316 Java \u51fd\u6570\u548c\u65b9\u6cd5\u7684\u6027\u80fd\uff1f<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u63d0\u9ad8\u51fd\u6570\u548c\u65b9\u6cd5\u7684\u6027\u80fd\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u6709\u52a9\u4e8e\u51cf\u5c11\u5ef6\u8fdf\u5e76\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u80fd\u529b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u6280\u5de7\uff0c\u9644\u6709\u5b9e\u9645\u793a\u4f8b\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n<p>\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u5982 int\u3001long \u548c double\uff09\u6bd4\u5bf9\u8c61\u7c7b\u578b\u66f4\u6709\u6548\u7387\uff0c\u56e0\u4e3a\u5b83\u4e0d\u9700\u8981\u5bf9\u8c61\u5f00\u9500\u3002<\/p>\n<ul>\n<li>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int count = 0;\nfor (int i = 0; i &lt; 1000000; i++) {\n  count++;\n}<\/pre>\n<\/li>\n<\/ul>\n<p><strong>2. \u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u591a\u6b21\u8ba1\u7b97\u76f8\u540c\u7684\u503c\uff0c\u8bf7\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\u5e76\u91cd\u590d\u4f7f\u7528\u3002<\/p>\n<ul>\n<li>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>double pi = Math.PI;\nfor (double angle = 0; angle &lt; 2 * pi; angle += pi \/ 100) {\n  \/\/ ...\n}<\/pre>\n<\/li>\n<\/ul>\n<p><strong>3. \u4f7f\u7528 StringBuilder \u4ee3\u66ff String<\/strong><\/p>\n<p>\u5bf9\u4e8e\u9891\u7e41\u7684\u64cd\u4f5c\u5b57\u7b26\u4e32\uff0cStringBuilder \u63d0\u4f9b\u4e86\u66f4\u6709\u6548\u7387\u7684\u65b9\u5f0f\u3002<\/p>\n<ul>\n<li>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>StringBuilder sb = new StringBuilder();\nfor (int i = 0; i &lt; 1000000; i++) {\n  sb.append(\"a\");\n}<\/pre>\n<\/li>\n<\/ul>\n<p><strong>4. \u4f18\u5316\u5faa\u73af<\/strong><\/p>\n<ul>\n<li><strong>\u4f7f\u7528 for-each \u5faa\u73af\uff1a<\/strong> \u5982\u679c\u8fed\u4ee3\u7684\u5bf9\u8c61\u6570\u7ec4\u5177\u6709\u5df2\u77e5\u7684\u957f\u5ea6\uff0c\u5219 for-each \u5faa\u73af\u6bd4 for \u5faa\u73af\u66f4\u6709\u6548\u7387\u3002<\/li>\n<li><strong>\u63d0\u524d\u89e3\u9664\u5faa\u73af\uff1a<\/strong> \u5982\u679c\u5faa\u73af\u6761\u4ef6\u4e0d\u9002\u5408\u6267\u884c\uff0c\u8bf7\u4f7f\u7528 break \u8bed\u53e5\u63d0\u524d\u89e3\u9664\u5faa\u73af\u3002<\/li>\n<li>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] array = new int[1000000];\nfor (int i = 0; i &lt; array.length; i++) {\n  if (array[i] == 0) {\n      break;\n  }\n}<\/pre>\n<\/li>\n<\/ul>\n<p><strong>5. \u4f7f\u7528\u6570\u636e\u7ed3\u6784<\/strong><\/p>\n<p>\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u63d0\u9ad8\u5bf9\u6570\u636e\u7684\u8bbf\u95ee\u6548\u7387\uff0c\u4ece\u800c\u4f18\u5316\u67e5\u627e\u548c\u68c0\u7d22\u64cd\u4f5c\u3002<\/p>\n<ul>\n<li>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();\nmap.put(\"key1\", 1);\nmap.put(\"key2\", 2);\nSystem.out.println(map.get(\"key1\"));<\/pre>\n<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u4f18\u5316\u67e5\u627e\u7b97\u6cd5<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b 100 \u4e07\u4e2a\u6574\u6570\u7684\u5217\u8868\uff0c\u9700\u8981\u67e5\u627e\u4e00\u4e2a\u7279\u5b9a\u7684\u6574\u6570\u3002<\/p>\n<ul>\n<li>\n<p><strong>\u672a\u4f18\u5316\u7b97\u6cd5\uff1a<\/strong><\/p>\n<pre>public int find(List&lt;Integer&gt; list, int target) {\n  for (int i = 0; i &lt; list.size(); i++) {\n      if (list.get(i) == target) {\n          return i;\n      }\n  }\n  return -1;\n}<\/pre>\n<p>\u6b64\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\uff0c\u5176\u4e2d n \u662f\u5217\u8868\u7684\u957f\u5ea6\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u5316\u7b97\u6cd5\uff0c\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\uff1a<\/strong><\/p>\n<pre>public int find(List&lt;Integer&gt; list, int target) {\n  int low = 0;\n  int high = list.size() - 1;\n  while (low &lt;= high) {\n      int mid = (low + high) \/ 2;\n      if (list.get(mid) == target) {\n          return mid;\n      } else if (list.get(mid) &lt; target) {\n          low = mid + 1;\n      } else {\n          high = mid - 1;\n      }\n  }\n  return -1;\n}<\/pre>\n<p>\u6b64\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(log n)\uff0c\u660e\u663e\u4f18\u4e8e\u672a\u4f18\u5316\u7b97\u6cd5\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f18\u5316 Java \u51fd\u6570\u548c\u65b9\u6cd5\u7684&#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-203074","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203074","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=203074"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203074\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}