{"id":36969,"date":"2024-11-26T12:02:29","date_gmt":"2024-11-26T04:02:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36969\/"},"modified":"2024-11-26T12:02:29","modified_gmt":"2024-11-26T04:02:29","slug":"java%e6%95%b0%e7%bb%84%e4%b8%8b%e6%a0%87%e6%80%8e%e4%b9%88%e6%89%be","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36969\/","title":{"rendered":"java\u6570\u7ec4\u4e0b\u6807\u600e\u4e48\u627e"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u83b7\u53d6\u6570\u7ec4\u4e0b\u6807\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a1. \u76f4\u63a5\u8d4b\u503c\uff1a\u5c06\u4e0b\u6807\u503c\u5206\u914d\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff1b2. \u4f7f\u7528 arrays.binarysearch() \u65b9\u6cd5\uff1a\u9002\u7528\u4e8e\u5df2\u6309\u5347\u5e8f\u6392\u5e8f\u7684\u6570\u7ec4\u3002\u4e0a\u8ff0\u65b9\u6cd5\u53ef\u5e2e\u52a9\u67e5\u627e\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u4e0b\u6807\uff0c\u8bbf\u95ee\u4e0d\u5b58\u5728\u4e0b\u6807\u4f1a\u5bfc\u81f4 indexoutofboundsexception\u3002\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u4e0b\u6807\u8868\u793a\u6bcf\u4e2a\u7ef4\u5ea6\u7684\u5143\u7d20\u4f4d\u7f6e\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111222310843604.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u4e0b\u6807\u600e\u4e48\u627e\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u4e0b\u6807\u600e\u4e48\u627e\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\u4e0b\u6807\u7684\u67e5\u627e<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u6570\u7ec4\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5b58\u50a8\u76f8\u540c\u7c7b\u578b\u7684\u6570\u636e\u5143\u7d20\u3002\u6bcf\u4e2a\u5143\u7d20\u90fd\u6709\u4e00\u4e2a\u4e0e\u4e4b\u76f8\u5173\u8054\u7684\u552f\u4e00\u4e0b\u6807\u3002\u4e0b\u6807\u4ece 0 \u5f00\u59cb\uff0c\u6700\u5927\u503c\u4e3a\u6570\u7ec4\u957f\u5ea6\u51cf\u4e00\u3002<\/p>\n<p><strong>\u786e\u5b9a\u6570\u7ec4\u4e0b\u6807\u7684\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\u6765\u67e5\u627e\u6570\u7ec4\u4e0b\u6807\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>1. \u76f4\u63a5\u8d4b\u503c<\/strong><\/p>\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u5c06\u4e0b\u6807\u503c\u5206\u914d\u7ed9\u4e00\u4e2a\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>int[] numbers = {1, 2, 3, 4, 5};\nint index = 2; \/\/ \u4e0b\u6807\u4e3a 2<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Arrays.binarySearch() \u65b9\u6cd5<\/strong><\/p>\n<p>\u5982\u679c\u6570\u7ec4\u5df2\u6309\u5347\u5e8f\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.binarySearch() \u65b9\u6cd5\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u5143\u7d20\u5e76\u8fd4\u56de\u5176\u4e0b\u6807\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>int[] sortedNumbers = {1, 2, 3, 4, 5};\nint index = Arrays.binarySearch(sortedNumbers, 3); \/\/ \u4e0b\u6807\u4e3a 2<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u8bd5\u56fe\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u4e0b\u6807\uff08\u8d85\u51fa\u4e86\u6570\u7ec4\u8303\u56f4\uff09\uff0c\u5c06\u5f15\u53d1 IndexOutOfBoundsException\u3002<\/li>\n<li>\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u4e0b\u6807\u8868\u793a\u6bcf\u4e2a\u7ef4\u5ea6\u7684\u5143\u7d20\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u4e0b\u6807\u600e\u4e48\u627e\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\u83b7\u53d6\u6570\u7ec4\u4e0b\u6807\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a1. \u76f4\u63a5\u8d4b\u503c\uff1a\u5c06\u4e0b\u6807\u503c\u5206\u914d\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff1b2. \u4f7f\u7528 arrays.binarysearch() \u65b9\u6cd5\uff1a\u9002\u7528\u4e8e\u5df2\u6309\u5347\u5e8f\u6392\u5e8f\u7684\u6570\u7ec4\u3002\u4e0a\u8ff0\u65b9\u6cd5\u53ef\u5e2e\u52a9\u67e5\u627e\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u4e0b\u6807\uff0c\u8bbf\u95ee\u4e0d\u5b58\u5728\u4e0b\u6807\u4f1a\u5bfc\u81f4 indexoutofboundsexception\u3002\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u4e0b\u6807\u8868\u793a\u6bcf\u4e2a\u7ef4\u5ea6\u7684\u5143\u7d20\u4f4d\u7f6e\u3002 Java \u6570\u7ec4\u4e0b\u6807\u7684\u67e5\u627e \u5728 Java \u4e2d\uff0c\u6570\u7ec4\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5b58\u50a8\u76f8\u540c\u7c7b\u578b\u7684\u6570\u636e\u5143\u7d20\u3002\u6bcf\u4e2a\u5143\u7d20\u90fd\u6709\u4e00\u4e2a\u4e0e\u4e4b\u76f8\u5173\u8054\u7684\u552f\u4e00\u4e0b\u6807\u3002\u4e0b\u6807\u4ece 0 \u5f00\u59cb\uff0c\u6700\u5927\u503c\u4e3a\u6570\u7ec4\u957f\u5ea6\u51cf\u4e00\u3002 \u786e\u5b9a\u6570\u7ec4\u4e0b\u6807\u7684\u65b9\u6cd5\uff1a \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\u6765\u67e5\u627e\u6570\u7ec4\u4e0b\u6807\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1. \u76f4\u63a5\u8d4b\u503c \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u5c06\u4e0b\u6807\u503c\u5206\u914d\u7ed9\u4e00\u4e2a\u53d8\u91cf\u3002\u4f8b\u5982\uff1a int[] numbers = {1, 2, 3, 4, 5}; int index = 2; \/\/ \u4e0b\u6807\u4e3a 2 \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 Arrays.binarySearch() \u65b9\u6cd5 \u5982\u679c\u6570\u7ec4\u5df2\u6309\u5347\u5e8f\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.binarySearch() \u65b9\u6cd5\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u5143\u7d20\u5e76\u8fd4\u56de\u5176\u4e0b\u6807\u3002\u4f8b\u5982\uff1a int[] sortedNumbers = {1, 2, 3, 4, 5}; int 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-36969","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36969","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=36969"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36969\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}