{"id":65910,"date":"2025-05-03T16:46:01","date_gmt":"2025-05-03T08:46:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65910\/"},"modified":"2025-05-03T16:46:01","modified_gmt":"2025-05-03T08:46:01","slug":"java%e6%80%8e%e4%b9%88%e6%89%be%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84%e4%b8%8b%e8%a1%a8-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65910\/","title":{"rendered":"java\u600e\u4e48\u627e\u6570\u7ec4\u4e2d\u7684\u4e0b\u8868"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u67e5\u627e\u6570\u7ec4\u4e2d\u4e0b\u6807\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a\u4f7f\u7528 arrays.binarysearch()\uff08\u5df2\u6392\u5e8f\u6570\u7ec4\uff09\u3001for \u5faa\u73af\uff08\u672a\u6392\u5e8f\u6216\u4e8c\u5206\u641c\u7d22\u4e0d\u9002\u7528\uff09\u548c apache commons lang\uff08\u9ad8\u7ea7\u529f\u80fd\uff09\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111315305163440.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u627e\u6570\u7ec4\u4e2d\u7684\u4e0b\u8868\u63d2\u56fe\" alt=\"java\u600e\u4e48\u627e\u6570\u7ec4\u4e2d\u7684\u4e0b\u8868\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u67e5\u627e\u6570\u7ec4\u4e2d\u7684\u4e0b\u6807<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u67e5\u627e\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u4e0b\u6807\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u51e0\u79cd\u65b9\u6cd5\u6765\u5b8c\u6210\u6b64\u4efb\u52a1\u3002<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 Arrays.binarySearch()<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u4f55\u65f6\u4f7f\u7528\uff1a<\/strong>\u5f53\u6570\u7ec4\u5df2\u6309\u5347\u5e8f\u6392\u5e8f\u65f6\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>int index = Arrays.binarySearch(array, element);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8fd4\u56de\u503c\uff1a<\/strong>\u5982\u679c\u627e\u5230\u5143\u7d20\uff0c\u5219\u8fd4\u56de\u5176\u4e0b\u6807\uff1b\u5426\u5219\u8fd4\u56de (-(\u63d2\u5165\u70b9) &#8211; 1)\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] array = {1, 2, 3, 5, 7, 9};\nint index = Arrays.binarySearch(array, 5);\nSystem.out.println(\"\u4e0b\u6807\uff1a\" + index); \/\/ \u8f93\u51fa\uff1a3<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 for \u5faa\u73af<\/strong><\/p>\n<p><strong>\u4f55\u65f6\u4f7f\u7528\uff1a<\/strong>\u5f53\u6570\u7ec4\u672a\u6392\u5e8f\u6216\u4f7f\u7528\u4e8c\u5206\u641c\u7d22\u4e0d\u5408\u9002\u65f6\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>for (int i = 0; i &lt; array.length; i++) {\n    if (array[i] == element) {\n        return i;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8fd4\u56de\u503c\uff1a<\/strong>\u5982\u679c\u627e\u5230\u5143\u7d20\uff0c\u5219\u8fd4\u56de\u5176\u4e0b\u6807\uff1b\u5426\u5219\u8fd4\u56de -1\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] array = {5, 2, 1, 9, 3, 7};\nint index = -1;\nfor (int i = 0; i &lt; array.length; i++) {\n    if (array[i] == 5) {\n        index = i;\n        break;\n    }\n}\nSystem.out.println(\"\u4e0b\u6807\uff1a\" + index); \/\/ \u8f93\u51fa\uff1a0<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 3\uff1a\u4f7f\u7528 Apache Commons Lang<\/strong><\/p>\n<p><strong>\u4f55\u65f6\u4f7f\u7528\uff1a<\/strong>\u5f53\u9700\u8981\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\u65f6\uff0c\u5982\u67e5\u627e\u6240\u6709\u5339\u914d\u5143\u7d20\u7684\u4e0b\u6807\u3002<\/p>\n<p><strong>\u5148\u51b3\u6761\u4ef6\uff1a<\/strong>\u5c06 commons-lang3 \u4f9d\u8d56\u9879\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>import org.apache.commons.lang3.ArrayUtils;\nint[] indices = ArrayUtils.indexesOf(array, element);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8fd4\u56de\u503c\uff1a<\/strong>\u5339\u914d\u5143\u7d20\u7684\u6240\u6709\u4e0b\u6807\u7684\u6570\u7ec4\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] array = {1, 5, 2, 5, 3, 7};\nint[] indices = ArrayUtils.indexesOf(array, 5);\nSystem.out.println(\"\u4e0b\u6807\uff1a\" + Arrays.toString(indices)); \/\/ \u8f93\u51fa\uff1a[1, 3]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u627e\u6570\u7ec4\u4e2d\u7684\u4e0b\u8868\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\u627e\u6570\u7ec4\u4e2d\u4e0b\u6807\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a\u4f7f\u7528 arrays.binarysearch()\uff08\u5df2\u6392\u5e8f\u6570\u7ec4\uff09\u3001for \u5faa\u73af\uff08\u672a\u6392\u5e8f\u6216\u4e8c\u5206\u641c\u7d22\u4e0d\u9002\u7528\uff09\u548c apache commons lang\uff08\u9ad8\u7ea7\u529f\u80fd\uff09\u3002 \u5982\u4f55\u5728 Java \u4e2d\u67e5\u627e\u6570\u7ec4\u4e2d\u7684\u4e0b\u6807 \u5f15\u8a00 \u5728 Java \u4e2d\uff0c\u67e5\u627e\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u4e0b\u6807\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u51e0\u79cd\u65b9\u6cd5\u6765\u5b8c\u6210\u6b64\u4efb\u52a1\u3002 \u65b9\u6cd5 1\uff1a\u4f7f\u7528 Arrays.binarySearch() \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4f55\u65f6\u4f7f\u7528\uff1a\u5f53\u6570\u7ec4\u5df2\u6309\u5347\u5e8f\u6392\u5e8f\u65f6\u3002 \u8bed\u6cd5\uff1a int index = Arrays.binarySearch(array, element); \u767b\u5f55\u540e\u590d\u5236 \u8fd4\u56de\u503c\uff1a\u5982\u679c\u627e\u5230\u5143\u7d20\uff0c\u5219\u8fd4\u56de\u5176\u4e0b\u6807\uff1b\u5426\u5219\u8fd4\u56de (-(\u63d2\u5165\u70b9) &#8211; 1)\u3002 \u793a\u4f8b\uff1a int[] array = {1, 2, 3, 5, 7, 9}; int index = Arrays.binarySearch(array, 5); System.out.println(&#8220;\u4e0b\u6807\uff1a&#8221; + index); \/\/ \u8f93\u51fa\uff1a3 \u767b\u5f55\u540e\u590d\u5236 \u65b9\u6cd5 2\uff1a\u4f7f\u7528 for [&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-65910","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65910","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=65910"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65910\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}