{"id":40155,"date":"2024-11-26T16:59:18","date_gmt":"2024-11-26T08:59:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40155\/"},"modified":"2024-11-26T16:59:18","modified_gmt":"2024-11-26T08:59:18","slug":"java%e6%80%8e%e4%b9%88%e5%88%a4%e6%96%ad%e4%b8%80%e4%b8%aa%e6%95%b0%e5%9c%a8%e6%95%b0%e7%bb%84%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/40155\/","title":{"rendered":"java\u600e\u4e48\u5224\u65ad\u4e00\u4e2a\u6570\u5728\u6570\u7ec4\u4e2d"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u5728\u6570\u7ec4\u4e2d\uff0c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u7ebf\u6027\u641c\u7d22\uff1a\u904d\u5386\u6570\u7ec4\u6bcf\u4e2a\u5143\u7d20\uff0c\u68c0\u67e5\u662f\u5426\u4e0e\u76ee\u6807\u6570\u5b57\u76f8\u7b49\u3002\u4e8c\u5206\u641c\u7d22\uff08\u6570\u7ec4\u5df2\u6392\u5e8f\uff09\uff1a\u5c06\u6570\u7ec4\u5206\u6210\u4e24\u534a\uff0c\u6839\u636e\u76ee\u6807\u6570\u5b57\u7684\u503c\u5728\u524d\u534a\u90e8\u5206\u6216\u540e\u534a\u90e8\u5206\u7ee7\u7eed\u641c\u7d22\u3002java 8 stream api\uff1a\u4f7f\u7528 stream()\u3001filter() \u548c findfirst() \u67e5\u627e\u7b2c\u4e00\u4e2a\u4e0e\u76ee\u6807\u6570\u5b57\u76f8\u7b49\u7684\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111721403233127.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5224\u65ad\u4e00\u4e2a\u6570\u5728\u6570\u7ec4\u4e2d\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5224\u65ad\u4e00\u4e2a\u6570\u5728\u6570\u7ec4\u4e2d\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u6570\u5728 Java \u6570\u7ec4\u4e2d<\/strong><\/p>\n<p>\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u5728\u4e00\u4e2a Java \u6570\u7ec4\u4e2d\u662f\u6570\u7ec4\u64cd\u4f5c\u4e2d\u7684\u4e00\u9879\u57fa\u672c\u4efb\u52a1\u3002\u4ee5\u4e0b\u5217\u51fa\u4e86\u51e0\u79cd\u5728 Java \u4e2d\u5224\u65ad\u4e00\u4e2a\u6570\u5728\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728\u7684\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u4f7f\u7528\u7ebf\u6027\u641c\u7d22<\/strong><\/p>\n<ul>\n<li>\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/li>\n<li>\u5bf9\u4e8e\u6bcf\u4e2a\u5143\u7d20\uff0c\u6bd4\u8f83\u5b83\u662f\u5426\u7b49\u4e8e\u8981\u67e5\u627e\u7684\u6570\u3002<\/li>\n<li>\u5982\u679c\u627e\u5230\u5339\u914d\u9879\uff0c\u5219\u8fd4\u56de true\u3002<\/li>\n<li>\u5982\u679c\u904d\u5386\u5b8c\u6574\u4e2a\u6570\u7ec4\u540e\u672a\u627e\u5230\u5339\u914d\u9879\uff0c\u5219\u8fd4\u56de false\u3002<\/li>\n<\/ul>\n<p><strong>Java \u4ee3\u7801\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>public static boolean linearSearch(int[] arr, int target) {\n    for (int i = 0; i &lt; arr.length; i++) {\n        if (arr[i] == target) {\n            return true;\n        }\n    }\n    return false;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u4e8c\u5206\u641c\u7d22\uff08\u524d\u63d0\u662f\u6570\u7ec4\u5df2\u6392\u5e8f\uff09<\/strong><\/p>\n<ul>\n<li>\u5c06\u6570\u7ec4\u5206\u6210\u4e24\u534a\u3002<\/li>\n<li>\u6bd4\u8f83\u8981\u67e5\u627e\u7684\u6570\u662f\u5426\u7b49\u4e8e\u4e2d\u95f4\u5143\u7d20\u3002<\/li>\n<li>\u5982\u679c\u7b49\u4e8e\uff0c\u5219\u8fd4\u56de true\u3002<\/li>\n<li>\u5982\u679c\u5c0f\u4e8e\uff0c\u5219\u5728\u524d\u534a\u90e8\u5206\u7ee7\u7eed\u641c\u7d22\u3002<\/li>\n<li>\u5982\u679c\u5927\u4e8e\uff0c\u5219\u5728\u540e\u534a\u90e8\u5206\u7ee7\u7eed\u641c\u7d22\u3002<\/li>\n<li>\u5982\u679c\u627e\u4e0d\u5230\u5339\u914d\u9879\uff0c\u5219\u8fd4\u56de false\u3002<\/li>\n<\/ul>\n<p><strong>Java \u4ee3\u7801\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>public static boolean binarySearch(int[] arr, int target) {\n    int left = 0;\n    int right = arr.length - 1;\n\n    while (left &lt;= right) {\n        int mid = (left + right) \/ 2;\n\n        if (arr[mid] == target) {\n            return true;\n        } else if (arr[mid] &lt; target) {\n            left = mid + 1;\n        } else {\n            right = mid - 1;\n        }\n    }\n    return false;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Java 8 Stream API<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 Arrays.stream(arr) \u521b\u5efa\u6570\u7ec4\u6d41\u3002<\/li>\n<li>\u4f7f\u7528 filter(n -&gt; n == target) \u8fc7\u6ee4\u51fa\u7b49\u4e8e\u76ee\u6807\u6570\u5b57\u7684\u5143\u7d20\u3002<\/li>\n<li>\u4f7f\u7528 findFirst() \u67e5\u627e\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002<\/li>\n<li>\u5982\u679c\u627e\u4e0d\u5230\u5339\u914d\u9879\uff0c\u5219\u8fd4\u56de Optional.empty()\u3002<\/li>\n<\/ul>\n<p><strong>Java \u4ee3\u7801\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>public static boolean streamSearch(int[] arr, int target) {\n    return Arrays.stream(arr)\n            .filter(n -&gt; n == target)\n            .findFirst()\n            .isPresent();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5224\u65ad\u4e00\u4e2a\u6570\u5728\u6570\u7ec4\u4e2d\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\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u5728\u6570\u7ec4\u4e2d\uff0c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u7ebf\u6027\u641c\u7d22\uff1a\u904d\u5386\u6570\u7ec4\u6bcf\u4e2a\u5143\u7d20\uff0c\u68c0\u67e5\u662f\u5426\u4e0e\u76ee\u6807\u6570\u5b57\u76f8\u7b49\u3002\u4e8c\u5206\u641c\u7d22\uff08\u6570\u7ec4\u5df2\u6392\u5e8f\uff09\uff1a\u5c06\u6570\u7ec4\u5206\u6210\u4e24\u534a\uff0c\u6839\u636e\u76ee\u6807\u6570\u5b57\u7684\u503c\u5728\u524d\u534a\u90e8\u5206\u6216\u540e\u534a\u90e8\u5206\u7ee7\u7eed\u641c\u7d22\u3002java 8 stream api\uff1a\u4f7f\u7528 stream()\u3001filter() \u548c findfirst() \u67e5\u627e\u7b2c\u4e00\u4e2a\u4e0e\u76ee\u6807\u6570\u5b57\u76f8\u7b49\u7684\u5143\u7d20\u3002 \u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u6570\u5728 Java \u6570\u7ec4\u4e2d \u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u5728\u4e00\u4e2a Java \u6570\u7ec4\u4e2d\u662f\u6570\u7ec4\u64cd\u4f5c\u4e2d\u7684\u4e00\u9879\u57fa\u672c\u4efb\u52a1\u3002\u4ee5\u4e0b\u5217\u51fa\u4e86\u51e0\u79cd\u5728 Java \u4e2d\u5224\u65ad\u4e00\u4e2a\u6570\u5728\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728\u7684\u65b9\u6cd5\uff1a \u4f7f\u7528\u7ebf\u6027\u641c\u7d22 \u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002 \u5bf9\u4e8e\u6bcf\u4e2a\u5143\u7d20\uff0c\u6bd4\u8f83\u5b83\u662f\u5426\u7b49\u4e8e\u8981\u67e5\u627e\u7684\u6570\u3002 \u5982\u679c\u627e\u5230\u5339\u914d\u9879\uff0c\u5219\u8fd4\u56de true\u3002 \u5982\u679c\u904d\u5386\u5b8c\u6574\u4e2a\u6570\u7ec4\u540e\u672a\u627e\u5230\u5339\u914d\u9879\uff0c\u5219\u8fd4\u56de false\u3002 Java \u4ee3\u7801\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b public static boolean linearSearch(int[] arr, int target) { for (int i = 0; i &lt; arr.length; i++) { if (arr[i] == target) { return true; } } [&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-40155","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40155","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=40155"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40155\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}