{"id":65742,"date":"2025-05-03T14:09:14","date_gmt":"2025-05-03T06:09:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65742\/"},"modified":"2025-05-03T14:09:14","modified_gmt":"2025-05-03T06:09:14","slug":"java%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%88%a4%e6%96%ad%e6%95%b0%e6%95%b0%e7%9b%b8%e7%ad%89-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65742\/","title":{"rendered":"java\u6570\u7ec4\u600e\u4e48\u5224\u65ad\u6570\u6570\u76f8\u7b49"},"content":{"rendered":"<blockquote><p>\n  \u5224\u65ad java \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u662f\u5426\u76f8\u7b49\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u76f4\u63a5\u6bd4\u8f83\u5143\u7d20\uff1a\u9010\u4e2a\u5143\u7d20\u6bd4\u8f83\uff0c\u5982\u679c\u6240\u6709\u5143\u7d20\u503c\u76f8\u7b49\uff0c\u5219\u76f8\u7b49\u3002\u4f7f\u7528 arrays.equals() \u65b9\u6cd5\uff1a\u9010\u4e2a\u5143\u7d20\u6bd4\u8f83\uff0c\u5982\u679c\u6240\u6709\u5143\u7d20\u76f8\u7b49\uff0c\u5219\u8fd4\u56de true\u3002\u4f7f\u7528\u6d41\uff1a\u4f7f\u7528 reduce() \u65b9\u6cd5\u5bf9\u5143\u7d20\u8fdb\u884c\u89c4\u7ea6\uff0c\u5982\u679c\u6240\u6709\u5143\u7d20\u76f8\u7b49\uff0c\u5219\u8fd4\u56de true\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111308521236094.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u600e\u4e48\u5224\u65ad\u6570\u6570\u76f8\u7b49\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u600e\u4e48\u5224\u65ad\u6570\u6570\u76f8\u7b49\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad Java \u6570\u7ec4\u4e2d\u5143\u7d20\u662f\u5426\u76f8\u7b49<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u6bd4\u8f83\u5143\u7d20\uff1a<\/strong><\/p>\n<p>\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u6bd4\u8f83\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\uff0c\u5982\u679c\u6240\u6709\u5143\u7d20\u7684\u503c\u90fd\u76f8\u7b49\uff0c\u5219\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u76f8\u7b49\u3002<\/p>\n<pre>public static boolean isAllEqual(int[] arr) {\n    for (int i = 1; i &lt; arr.length; i++) {\n        if (arr[i] != arr[0]) {\n            return false;\n        }\n    }\n    return true;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Arrays.equals()\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>Java \u63d0\u4f9b\u4e86 Arrays.equals() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u662f\u5426\u76f8\u7b49\u3002\u6b64\u65b9\u6cd5\u6bd4\u8f83\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u6240\u6709\u5143\u7d20\u76f8\u7b49\uff0c\u5219\u8fd4\u56de true\u3002<\/p>\n<pre>public static boolean isAllEqual(int[] arr) {\n    return Arrays.equals(arr, arr[0]);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u6d41\uff1a<\/strong><\/p>\n<p>Java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u5f15\u5165\u4e86\u6d41 API\uff0c\u53ef\u4ee5\u7528\u5b83\u6765\u7b80\u6d01\u5730\u5904\u7406\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 reduce() \u65b9\u6cd5\u6765\u5bf9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u89c4\u7ea6\uff0c\u5e76\u68c0\u67e5\u662f\u5426\u6240\u6709\u5143\u7d20\u90fd\u76f8\u7b49\u3002<\/p>\n<pre>public static boolean isAllEqual(int[] arr) {\n    return Arrays.stream(arr).reduce((a, b) -&gt; a == b ? a : -1).isPresent();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u600e\u4e48\u5224\u65ad\u6570\u6570\u76f8\u7b49\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>\u5224\u65ad java \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u662f\u5426\u76f8\u7b49\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u76f4\u63a5\u6bd4\u8f83\u5143\u7d20\uff1a\u9010\u4e2a\u5143\u7d20\u6bd4\u8f83\uff0c\u5982\u679c\u6240\u6709\u5143\u7d20\u503c\u76f8\u7b49\uff0c\u5219\u76f8\u7b49\u3002\u4f7f\u7528 arrays.equals() \u65b9\u6cd5\uff1a\u9010\u4e2a\u5143\u7d20\u6bd4\u8f83\uff0c\u5982\u679c\u6240\u6709\u5143\u7d20\u76f8\u7b49\uff0c\u5219\u8fd4\u56de true\u3002\u4f7f\u7528\u6d41\uff1a\u4f7f\u7528 reduce() \u65b9\u6cd5\u5bf9\u5143\u7d20\u8fdb\u884c\u89c4\u7ea6\uff0c\u5982\u679c\u6240\u6709\u5143\u7d20\u76f8\u7b49\uff0c\u5219\u8fd4\u56de true\u3002 \u5982\u4f55\u5224\u65ad Java \u6570\u7ec4\u4e2d\u5143\u7d20\u662f\u5426\u76f8\u7b49 \u76f4\u63a5\u6bd4\u8f83\u5143\u7d20\uff1a \u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u6bd4\u8f83\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\uff0c\u5982\u679c\u6240\u6709\u5143\u7d20\u7684\u503c\u90fd\u76f8\u7b49\uff0c\u5219\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u76f8\u7b49\u3002 public static boolean isAllEqual(int[] arr) { for (int i = 1; i &lt; arr.length; i++) { if (arr[i] != arr[0]) { return false; } } return true; } \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 Arrays.equals()\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b Java \u63d0\u4f9b\u4e86 Arrays.equals() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u662f\u5426\u76f8\u7b49\u3002\u6b64\u65b9\u6cd5\u6bd4\u8f83\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u6240\u6709\u5143\u7d20\u76f8\u7b49\uff0c\u5219\u8fd4\u56de true\u3002 public static boolean isAllEqual(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-65742","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65742","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=65742"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65742\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}