{"id":65819,"date":"2025-05-03T12:17:03","date_gmt":"2025-05-03T04:17:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65819\/"},"modified":"2025-05-03T12:17:03","modified_gmt":"2025-05-03T04:17:03","slug":"java%e6%80%8e%e4%b9%88%e6%9f%a5%e8%af%a2%e6%95%b0%e7%bb%84%e7%9a%84%e4%b8%8b%e6%a0%87-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65819\/","title":{"rendered":"java\u600e\u4e48\u67e5\u8be2\u6570\u7ec4\u7684\u4e0b\u6807"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u67e5\u8be2\u6570\u7ec4\u4e0b\u6807\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\u7ebf\u6027\u641c\u7d22\uff0c\u904d\u5386\u6570\u7ec4\u5143\u7d20\u4e0e\u76ee\u6807\u5143\u7d20\u9010\u4e00\u5bf9\u6bd4\u3002\u4e8c\u5206\u641c\u7d22\uff08\u4ec5\u9650\u6709\u5e8f\u6570\u7ec4\uff09\uff0c\u901a\u8fc7\u4e0d\u65ad\u4e8c\u5206\u7f29\u5c0f\u641c\u7d22\u8303\u56f4\uff0c\u63d0\u5347\u6548\u7387\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111311304438201.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u67e5\u8be2\u6570\u7ec4\u7684\u4e0b\u6807\u63d2\u56fe\" alt=\"java\u600e\u4e48\u67e5\u8be2\u6570\u7ec4\u7684\u4e0b\u6807\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u67e5\u8be2\u6570\u7ec4\u7684\u4e0b\u6807<\/strong><\/p>\n<p>Java \u4e2d\u67e5\u627e\u6570\u7ec4\u5143\u7d20\u4e0b\u6807\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528\u7ebf\u6027\u641c\u7d22<\/strong><\/p>\n<p>\u7ebf\u6027\u641c\u7d22\u4f9d\u6b21\u904d\u5386\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e0e\u76ee\u6807\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de\u5176\u4e0b\u6807\u3002\u4ee5\u4e0b\u662f\u7ebf\u6027\u641c\u7d22\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>public int findIndex(int[] arr, int target) {\n  for (int i = 0; i &lt; arr.length; i++) {\n    if (arr[i] == target) {\n      return i;\n    }\n  }\n  return -1; \/\/ \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u4e8c\u5206\u641c\u7d22\uff08\u4ec5\u9650\u6709\u5e8f\u6570\u7ec4\uff09<\/strong><\/p>\n<p>\u5982\u679c\u6570\u7ec4\u662f\u6709\u5e8f\u7684\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u4e8c\u5206\u641c\u7d22\u7b97\u6cd5\u6765\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u3002\u4e8c\u5206\u641c\u7d22\u901a\u8fc7\u5c06\u6570\u7ec4\u4e0d\u65ad\u5730\u5206\u6210\u4e24\u534a\uff0c\u7f29\u5c0f\u641c\u7d22\u8303\u56f4\u3002\u4ee5\u4e0b\u662f\u4e8c\u5206\u641c\u7d22\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>public int binarySearch(int[] arr, int target) {\n  int low = 0;\n  int high = arr.length - 1;\n\n  while (low &lt;= high) {\n    int mid = low + (high - low) \/ 2;\n    if (arr[mid] == target) {\n      return mid;\n    } else if (arr[mid] &lt; target) {\n      low = mid + 1;\n    } else {\n      high = mid - 1;\n    }\n  }\n  return -1; \/\/ \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u67e5\u8be2\u6570\u7ec4\u7684\u4e0b\u6807\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\u67e5\u8be2\u6570\u7ec4\u4e0b\u6807\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\u7ebf\u6027\u641c\u7d22\uff0c\u904d\u5386\u6570\u7ec4\u5143\u7d20\u4e0e\u76ee\u6807\u5143\u7d20\u9010\u4e00\u5bf9\u6bd4\u3002\u4e8c\u5206\u641c\u7d22\uff08\u4ec5\u9650\u6709\u5e8f\u6570\u7ec4\uff09\uff0c\u901a\u8fc7\u4e0d\u65ad\u4e8c\u5206\u7f29\u5c0f\u641c\u7d22\u8303\u56f4\uff0c\u63d0\u5347\u6548\u7387\u3002 \u5982\u4f55\u5728 Java \u4e2d\u67e5\u8be2\u6570\u7ec4\u7684\u4e0b\u6807 Java \u4e2d\u67e5\u627e\u6570\u7ec4\u5143\u7d20\u4e0b\u6807\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a 1. \u4f7f\u7528\u7ebf\u6027\u641c\u7d22 \u7ebf\u6027\u641c\u7d22\u4f9d\u6b21\u904d\u5386\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e0e\u76ee\u6807\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de\u5176\u4e0b\u6807\u3002\u4ee5\u4e0b\u662f\u7ebf\u6027\u641c\u7d22\u7684\u4ee3\u7801\u793a\u4f8b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b public int findIndex(int[] arr, int target) { for (int i = 0; i &lt; arr.length; i++) { if (arr[i] == target) { return i; } } return -1; \/\/ \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20 } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528\u4e8c\u5206\u641c\u7d22\uff08\u4ec5\u9650\u6709\u5e8f\u6570\u7ec4\uff09 \u5982\u679c\u6570\u7ec4\u662f\u6709\u5e8f\u7684\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u4e8c\u5206\u641c\u7d22\u7b97\u6cd5\u6765\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u3002\u4e8c\u5206\u641c\u7d22\u901a\u8fc7\u5c06\u6570\u7ec4\u4e0d\u65ad\u5730\u5206\u6210\u4e24\u534a\uff0c\u7f29\u5c0f\u641c\u7d22\u8303\u56f4\u3002\u4ee5\u4e0b\u662f\u4e8c\u5206\u641c\u7d22\u7684\u4ee3\u7801\u793a\u4f8b\uff1a public int binarySearch(int[] arr, int target) { int [&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-65819","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65819","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=65819"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65819\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}