{"id":64133,"date":"2025-05-03T15:54:34","date_gmt":"2025-05-03T07:54:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64133\/"},"modified":"2025-05-03T15:54:34","modified_gmt":"2025-05-03T07:54:34","slug":"java%e6%95%b0%e7%bb%84%e9%87%8c%e6%80%8e%e4%b9%88%e8%be%93%e5%87%ban%e7%9a%84%e4%b8%8b%e6%a0%87-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64133\/","title":{"rendered":"java\u6570\u7ec4\u91cc\u600e\u4e48\u8f93\u51fan\u7684\u4e0b\u6807"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u6570\u7ec4\u4e2d\u83b7\u53d6\u5143\u7d20\u4e0b\u6807\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a\u4f7f\u7528\u6570\u7ec4\u7684\u4e0b\u6807\u8fd0\u7b97\u7b26\u4f7f\u7528 arrays.binarysearch() \u65b9\u6cd5\u4f7f\u7528 for-each \u5faa\u73af\u548c\u7d22\u5f15\u53d8\u91cf\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/02\/2024110214484431151.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u91cc\u600e\u4e48\u8f93\u51fan\u7684\u4e0b\u6807\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u91cc\u600e\u4e48\u8f93\u51fan\u7684\u4e0b\u6807\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u6570\u7ec4\u4e2d\u83b7\u53d6\u5143\u7d20\u4e0b\u6807<\/strong><\/p>\n<p>\u5728Java\u4e2d\uff0c\u6570\u7ec4\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u4e0b\u6807\u6765\u8bbf\u95ee\u548c\u4fee\u6539\u3002\u4e0b\u6807\u4ece0\u5f00\u59cb\uff0c\u8868\u793a\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u4f4d\u7f6e\u3002\u83b7\u53d6\u6570\u7ec4\u5143\u7d20\u7684\u4e0b\u6807\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\uff1a<\/p>\n<p><strong>\u4e00\u3001\u4f7f\u7528\u6570\u7ec4\u7684\u4e0b\u6807\u8fd0\u7b97\u7b26<\/strong><\/p>\n<pre>int[] myArray = {1, 2, 3, 4, 5};\nint n = myArray[2];\nSystem.out.println(n); \/\/ \u8f93\u51fa\uff1a3<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e8c\u3001\u4f7f\u7528Arrays.binarySearch() \u65b9\u6cd5<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5bf9\u4e8e\u7ecf\u8fc7\u6392\u5e8f\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.binarySearch() \u65b9\u6cd5\u6765\u67e5\u627e\u67d0\u4e2a\u5143\u7d20\u7684\u4e0b\u6807\u3002<\/p>\n<pre>int[] myArray = {1, 2, 3, 4, 5};\nArrays.sort(myArray); \/\/ \u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\nint n = Arrays.binarySearch(myArray, 4);\nSystem.out.println(n); \/\/ \u8f93\u51fa\uff1a3<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e09\u3001\u4f7f\u7528for-each \u5faa\u73af\u548c\u7d22\u5f15\u53d8\u91cf<\/strong><\/p>\n<pre>int[] myArray = {1, 2, 3, 4, 5};\nfor (int i = 0; i &lt; myArray.length; i++) {\n    if (myArray[i] == n) {\n        System.out.println(i); \/\/ \u8f93\u51fa\uff1a3\n        break;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u91cc\u600e\u4e48\u8f93\u51fan\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 \u6570\u7ec4\u4e2d\u83b7\u53d6\u5143\u7d20\u4e0b\u6807\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a\u4f7f\u7528\u6570\u7ec4\u7684\u4e0b\u6807\u8fd0\u7b97\u7b26\u4f7f\u7528 arrays.binarysearch() \u65b9\u6cd5\u4f7f\u7528 for-each \u5faa\u73af\u548c\u7d22\u5f15\u53d8\u91cf Java\u6570\u7ec4\u4e2d\u83b7\u53d6\u5143\u7d20\u4e0b\u6807 \u5728Java\u4e2d\uff0c\u6570\u7ec4\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u4e0b\u6807\u6765\u8bbf\u95ee\u548c\u4fee\u6539\u3002\u4e0b\u6807\u4ece0\u5f00\u59cb\uff0c\u8868\u793a\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u4f4d\u7f6e\u3002\u83b7\u53d6\u6570\u7ec4\u5143\u7d20\u7684\u4e0b\u6807\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\uff1a \u4e00\u3001\u4f7f\u7528\u6570\u7ec4\u7684\u4e0b\u6807\u8fd0\u7b97\u7b26 int[] myArray = {1, 2, 3, 4, 5}; int n = myArray[2]; System.out.println(n); \/\/ \u8f93\u51fa\uff1a3 \u767b\u5f55\u540e\u590d\u5236 \u4e8c\u3001\u4f7f\u7528Arrays.binarySearch() \u65b9\u6cd5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5bf9\u4e8e\u7ecf\u8fc7\u6392\u5e8f\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.binarySearch() \u65b9\u6cd5\u6765\u67e5\u627e\u67d0\u4e2a\u5143\u7d20\u7684\u4e0b\u6807\u3002 int[] myArray = {1, 2, 3, 4, 5}; Arrays.sort(myArray); \/\/ \u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f int n = Arrays.binarySearch(myArray, 4); System.out.println(n); \/\/ \u8f93\u51fa\uff1a3 \u767b\u5f55\u540e\u590d\u5236 \u4e09\u3001\u4f7f\u7528for-each \u5faa\u73af\u548c\u7d22\u5f15\u53d8\u91cf 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-64133","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64133","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=64133"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64133\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}