{"id":37404,"date":"2024-11-26T17:57:40","date_gmt":"2024-11-26T09:57:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37404\/"},"modified":"2024-11-26T17:57:40","modified_gmt":"2024-11-26T09:57:40","slug":"java%e9%87%8c%e9%9d%a2%e6%80%8e%e4%b9%88%e8%8e%b7%e5%8f%96%e6%95%b0%e7%bb%84%e4%b8%8b%e6%a0%87","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37404\/","title":{"rendered":"java\u91cc\u9762\u600e\u4e48\u83b7\u53d6\u6570\u7ec4\u4e0b\u6807"},"content":{"rendered":"<p><strong>\u5728 java \u4e2d\u83b7\u53d6\u6570\u7ec4\u4e0b\u6807<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e0b\u6807\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 length \u5c5e\u6027<\/strong><\/p>\n<pre>int[] numbers = {1, 2, 3, 4, 5};\nint length = numbers.length; \/\/ \u83b7\u53d6\u6570\u7ec4\u957f\u5ea6<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 for-each \u5faa\u73af<\/strong><\/p>\n<pre>int[] numbers = {1, 2, 3, 4, 5};\nint index = 0;\nfor (int number : numbers) {\n    System.out.println(\"Index: \" + index + \", Value: \" + number);\n    index++;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Arrays.binarySearch() \u65b9\u6cd5\uff08\u6709\u5e8f\u6570\u7ec4\uff09<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5982\u679c\u6570\u7ec4\u662f\u6709\u5e8f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.binarySearch() \u65b9\u6cd5\u5feb\u901f\u67e5\u627e\u5143\u7d20\u5e76\u8fd4\u56de\u5176\u4e0b\u6807\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\u5219\u8fd4\u56de\u8d1f\u6570\u3002<\/p>\n<pre>int[] numbers = {1, 2, 3, 4, 5};\nint index = Arrays.binarySearch(numbers, 3); \/\/ \u8fd4\u56de 2\uff0c\u8868\u793a\u5143\u7d20 3 \u5728\u4e0b\u6807 2 \u5904<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528 customIndexOf() \u65b9\u6cd5<\/strong><\/p>\n<p>\u8fd8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684 customIndexOf() \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4f7f\u7528\u7ebf\u6027\u641c\u7d22\u6765\u67e5\u627e\u5143\u7d20\u7684\u4e0b\u6807\uff1a<\/p>\n<pre>public static int customIndexOf(int[] arr, int element) {\n    for (int i = 0; i &lt; arr.length; i++) {\n        if (arr[i] == element) {\n            return i;\n        }\n    }\n    return -1; \/\/ \u5982\u679c\u672a\u627e\u5230\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u91cc\u9762\u600e\u4e48\u83b7\u53d6\u6570\u7ec4\u4e0b\u6807\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\u83b7\u53d6\u6570\u7ec4\u4e0b\u6807 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e0b\u6807\uff1a 1. \u4f7f\u7528 length \u5c5e\u6027 int[] numbers = {1, 2, 3, 4, 5}; int length = numbers.length; \/\/ \u83b7\u53d6\u6570\u7ec4\u957f\u5ea6 \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 for-each \u5faa\u73af int[] numbers = {1, 2, 3, 4, 5}; int index = 0; for (int number : numbers) { System.out.println(&#8220;Index: &#8221; + index + &#8220;, Value: &#8221; [&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-37404","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37404","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=37404"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37404\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}