{"id":65528,"date":"2025-05-03T11:33:01","date_gmt":"2025-05-03T03:33:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65528\/"},"modified":"2025-05-03T11:33:01","modified_gmt":"2025-05-03T03:33:01","slug":"java%e6%80%8e%e4%b9%88%e9%80%9a%e8%bf%87%e6%95%b0%e7%bb%84%e5%85%83%e7%b4%a0%e6%89%be%e5%88%b0%e6%95%b0%e7%bb%84%e4%b8%8b%e6%a0%87-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65528\/","title":{"rendered":"java\u600e\u4e48\u901a\u8fc7\u6570\u7ec4\u5143\u7d20\u627e\u5230\u6570\u7ec4\u4e0b\u6807"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7java.util.arrays\u7c7b\u4e2d\u7684binarysearch()\u65b9\u6cd5\uff0c\u53ef\u5728\u6392\u5e8f\u6570\u7ec4\u4e2d\u67e5\u627e\u5143\u7d20\u4e0b\u6807\u3002\u6b64\u65b9\u6cd5\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\uff0c\u5feb\u901f\u9ad8\u6548\u5730\u8fd4\u56de\u76ee\u6807\u5143\u7d20\u7684\u4e0b\u6807\u6216\u63d2\u5165\u70b9\uff08\u8d1f\u6570\u8868\u793a\uff09\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111303005423123.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u901a\u8fc7\u6570\u7ec4\u5143\u7d20\u627e\u5230\u6570\u7ec4\u4e0b\u6807\u63d2\u56fe\" alt=\"java\u600e\u4e48\u901a\u8fc7\u6570\u7ec4\u5143\u7d20\u627e\u5230\u6570\u7ec4\u4e0b\u6807\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u901a\u8fc7\u6570\u7ec4\u5143\u7d20\u627e\u5230\u6570\u7ec4\u4e0b\u6807<\/strong><\/p>\n<p>\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528java.util.Arrays\u7c7b\u7684binarySearch()\u65b9\u6cd5\u6765\u901a\u8fc7\u6570\u7ec4\u5143\u7d20\u627e\u5230\u6570\u7ec4\u4e0b\u6807\u3002\u8be5\u65b9\u6cd5\u91c7\u7528\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u627e\u5230\u76ee\u6807\u5143\u7d20\u5728\u6392\u5e8f\u6570\u7ec4\u4e2d\u7684\u4e0b\u6807\u3002<\/p>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u8981\u4f7f\u7528binarySearch()\u65b9\u6cd5\uff0c\u9700\u8981\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u6570\u7ec4\u5fc5\u987b\u662f\u6392\u5e8f\u7684\u3002<\/li>\n<li>\u76ee\u6807\u5143\u7d20\u5fc5\u987b\u5728\u6570\u7ec4\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>int binarySearch(int[] arr, int key)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u53c2\u6570\uff1a<\/strong><\/p>\n<ul>\n<li>arr &#8211; \u8981\u641c\u7d22\u7684\u6392\u5e8f\u6570\u7ec4<\/li>\n<li>key &#8211; \u8981\u67e5\u627e\u7684\u76ee\u6807\u5143\u7d20<\/li>\n<\/ul>\n<p><strong>\u8fd4\u56de\u503c\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u4e0b\u6807\u3002<\/li>\n<li>\u5982\u679c\u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u4e00\u4e2a\u8d1f\u6570\uff0c\u8be5\u8d1f\u6570\u7684\u7edd\u5bf9\u503c\u8868\u793a\u76ee\u6807\u5143\u7d20\u5e94\u8be5\u63d2\u5165\u7684\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] arr = {1, 3, 5, 7, 9, 11};\nint key = 5;\n\nint index = Arrays.binarySearch(arr, key);\nSystem.out.println(\"Index of \" + key + \": \" + index); \/\/ \u8f93\u51fa\uff1a2<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u6570\u7ec4\u672a\u6392\u5e8f\uff0c\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u5c06\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\uff0cbinarySearch()\u65b9\u6cd5\u53ef\u80fd\u4f1a\u8fd4\u56de\u4e0d\u6b63\u786e\u7684\u7ed3\u679c\u3002<\/li>\n<li>\u5982\u679c\u76ee\u6807\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u51fa\u73b0\u591a\u6b21\uff0cbinarySearch()\u65b9\u6cd5\u4ec5\u8fd4\u56de\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u4e0b\u6807\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u901a\u8fc7\u6570\u7ec4\u5143\u7d20\u627e\u5230\u6570\u7ec4\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>\u901a\u8fc7java.util.arrays\u7c7b\u4e2d\u7684binarysearch()\u65b9\u6cd5\uff0c\u53ef\u5728\u6392\u5e8f\u6570\u7ec4\u4e2d\u67e5\u627e\u5143\u7d20\u4e0b\u6807\u3002\u6b64\u65b9\u6cd5\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\uff0c\u5feb\u901f\u9ad8\u6548\u5730\u8fd4\u56de\u76ee\u6807\u5143\u7d20\u7684\u4e0b\u6807\u6216\u63d2\u5165\u70b9\uff08\u8d1f\u6570\u8868\u793a\uff09\u3002 \u5982\u4f55\u901a\u8fc7\u6570\u7ec4\u5143\u7d20\u627e\u5230\u6570\u7ec4\u4e0b\u6807 \u5728Java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528java.util.Arrays\u7c7b\u7684binarySearch()\u65b9\u6cd5\u6765\u901a\u8fc7\u6570\u7ec4\u5143\u7d20\u627e\u5230\u6570\u7ec4\u4e0b\u6807\u3002\u8be5\u65b9\u6cd5\u91c7\u7528\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u627e\u5230\u76ee\u6807\u5143\u7d20\u5728\u6392\u5e8f\u6570\u7ec4\u4e2d\u7684\u4e0b\u6807\u3002 \u4f7f\u7528\u65b9\u6cd5\uff1a \u8981\u4f7f\u7528binarySearch()\u65b9\u6cd5\uff0c\u9700\u8981\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u6570\u7ec4\u5fc5\u987b\u662f\u6392\u5e8f\u7684\u3002 \u76ee\u6807\u5143\u7d20\u5fc5\u987b\u5728\u6570\u7ec4\u4e2d\u3002 \u8bed\u6cd5\uff1a int binarySearch(int[] arr, int key) \u767b\u5f55\u540e\u590d\u5236 \u53c2\u6570\uff1a arr &#8211; \u8981\u641c\u7d22\u7684\u6392\u5e8f\u6570\u7ec4 key &#8211; \u8981\u67e5\u627e\u7684\u76ee\u6807\u5143\u7d20 \u8fd4\u56de\u503c\uff1a \u5982\u679c\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u4e0b\u6807\u3002 \u5982\u679c\u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u4e00\u4e2a\u8d1f\u6570\uff0c\u8be5\u8d1f\u6570\u7684\u7edd\u5bf9\u503c\u8868\u793a\u76ee\u6807\u5143\u7d20\u5e94\u8be5\u63d2\u5165\u7684\u4f4d\u7f6e\u3002 \u793a\u4f8b\uff1a int[] arr = {1, 3, 5, 7, 9, 11}; int key = 5; int index = Arrays.binarySearch(arr, key); System.out.println(&#8220;Index of &#8221; + key + &#8220;: &#8221; + index); \/\/ [&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-65528","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65528","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=65528"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65528\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}