{"id":36908,"date":"2024-11-26T11:23:44","date_gmt":"2024-11-26T03:23:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36908\/"},"modified":"2024-11-26T11:23:44","modified_gmt":"2024-11-26T03:23:44","slug":"java%e6%80%8e%e4%b9%88%e6%b1%82%e6%95%b0%e7%bb%84%e7%b4%a2%e5%bc%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36908\/","title":{"rendered":"java\u600e\u4e48\u6c42\u6570\u7ec4\u7d22\u5f15"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u67e5\u627e\u6570\u7ec4\u7d22\u5f15\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u76f4\u63a5\u8bbf\u95ee\uff1a\u901a\u8fc7\u4e0b\u6807\u8bbf\u95ee\u5143\u7d20\uff0c\u901f\u5ea6\u6700\u5feb\u3002\u7ebf\u6027\u641c\u7d22\uff1a\u9010\u4e2a\u5143\u7d20\u6bd4\u8f83\u67e5\u627e\uff0c\u9002\u7528\u4e8e\u672a\u6392\u5e8f\u6570\u7ec4\u3002\u4e8c\u5206\u641c\u7d22\uff1a\u9002\u7528\u4e8e\u5df2\u6392\u5e8f\u6570\u7ec4\uff0c\u6548\u7387\u66f4\u9ad8\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111221220789833.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u6c42\u6570\u7ec4\u7d22\u5f15\u63d2\u56fe\" alt=\"java\u600e\u4e48\u6c42\u6570\u7ec4\u7d22\u5f15\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u67e5\u627e\u6570\u7ec4\u7d22\u5f15<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u8bbf\u95ee<\/strong><\/p>\n<p>\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u4e0b\u6807\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint element = arr[2]; \/\/ element = 3<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7ebf\u6027\u641c\u7d22<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5982\u679c\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ebf\u6027\u641c\u7d22\u67e5\u627e\u5143\u7d20\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint element = 3;\nint index = -1;\n\nfor (int i = 0; i &lt; arr.length; i++) {\n    if (arr[i] == element) {\n        index = i;\n        break;\n    }\n}\n\nif (index == -1) {\n    System.out.println(\"\u5143\u7d20\u672a\u627e\u5230\");\n} else {\n    System.out.println(\"\u5143\u7d20\u5728\u7d22\u5f15 \" + index + \" \u627e\u5230\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e8c\u5206\u641c\u7d22<\/strong><\/p>\n<p>\u5bf9\u4e8e\u5df2\u6392\u5e8f\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u91c7\u7528\u4e8c\u5206\u641c\u7d22\u6765\u63d0\u9ad8\u6548\u7387\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint element = 3;\nint low = 0;\nint high = arr.length - 1;\n\nwhile (low &lt;= high) {\n    int mid = (low + high) \/ 2;\n\n    if (arr[mid] == element) {\n        return mid;\n    } else if (arr[mid] &lt; element) {\n        low = mid + 1;\n    } else {\n        high = mid - 1;\n    }\n}\n\nreturn -1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u6c42\u6570\u7ec4\u7d22\u5f15\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>\u5728 java \u4e2d\u67e5\u627e\u6570\u7ec4\u7d22\u5f15\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u76f4\u63a5\u8bbf\u95ee\uff1a\u901a\u8fc7\u4e0b\u6807\u8bbf\u95ee\u5143\u7d20\uff0c\u901f\u5ea6\u6700\u5feb\u3002\u7ebf\u6027\u641c\u7d22\uff1a\u9010\u4e2a\u5143\u7d20\u6bd4\u8f83\u67e5\u627e\uff0c\u9002\u7528\u4e8e\u672a\u6392\u5e8f\u6570\u7ec4\u3002\u4e8c\u5206\u641c\u7d22\uff1a\u9002\u7528\u4e8e\u5df2\u6392\u5e8f\u6570\u7ec4\uff0c\u6548\u7387\u66f4\u9ad8\u3002 \u5982\u4f55\u5728 Java \u4e2d\u67e5\u627e\u6570\u7ec4\u7d22\u5f15 \u76f4\u63a5\u8bbf\u95ee \u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u4e0b\u6807\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff0c\u5982\u4e0b\u6240\u793a\uff1a int[] arr = {1, 2, 3, 4, 5}; int element = arr[2]; \/\/ element = 3 \u767b\u5f55\u540e\u590d\u5236 \u7ebf\u6027\u641c\u7d22 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5982\u679c\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ebf\u6027\u641c\u7d22\u67e5\u627e\u5143\u7d20\uff1a int[] arr = {1, 2, 3, 4, 5}; int element = 3; int index = -1; for (int i = 0; i &lt; arr.length; i++) { if [&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-36908","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36908","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=36908"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36908\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}