{"id":39463,"date":"2024-11-26T11:48:15","date_gmt":"2024-11-26T03:48:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39463\/"},"modified":"2024-11-26T11:48:15","modified_gmt":"2024-11-26T03:48:15","slug":"java%e6%80%8e%e4%b9%88%e6%af%94%e8%be%83%e4%b8%a4%e4%b8%aa%e6%95%b0%e7%bb%84%e7%9b%b8%e7%ad%89","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39463\/","title":{"rendered":"Java\u600e\u4e48\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u76f8\u7b49"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u662f\u5426\u76f8\u7b49\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a\u4f7f\u7528 arrays.equals() \u65b9\u6cd5\u8fdb\u884c\u9010\u4e2a\u5143\u7d20\u6bd4\u8f83\u5bf9\u5143\u7d20\u4e3a\u5bf9\u8c61\u7684\u6570\u7ec4\u8fdb\u884c\u9010\u4e2a\u5143\u7d20\u6bd4\u8f83\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\uff08\u5982 guava\uff09\u7684\u65b9\u6cd5\u8fdb\u884c\u6bd4\u8f83\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111611160348311.jpg\" class=\"aligncenter\" title=\"Java\u600e\u4e48\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u76f8\u7b49\u63d2\u56fe\" alt=\"Java\u600e\u4e48\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u76f8\u7b49\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u6bd4\u8f83\u4e24\u4e2a Java \u6570\u7ec4\u662f\u5426\u76f8\u7b49<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u662f\u5426\u76f8\u7b49\uff1a<\/p>\n<p><strong>1. Arrays.equals() \u65b9\u6cd5<\/strong><\/p>\n<p>Arrays.equals() \u65b9\u6cd5\u662f\u6bd4\u8f83\u6570\u7ec4\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u3002\u5b83\u9010\u4e2a\u5143\u7d20\u5730\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\uff0c\u5982\u679c\u6240\u6709\u5143\u7d20\u90fd\u76f8\u7b49\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] arr1 = {1, 2, 3};\nint[] arr2 = {1, 2, 3};\n\nboolean isEqual = Arrays.equals(arr1, arr2);\n\nif (isEqual) {\n  System.out.println(\"\u4e24\u4e2a\u6570\u7ec4\u76f8\u7b49\");\n} else {\n  System.out.println(\"\u4e24\u4e2a\u6570\u7ec4\u4e0d\u76f8\u7b49\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u9010\u4e2a\u5143\u7d20\u6bd4\u8f83<\/strong><\/p>\n<p>\u5982\u679c\u6570\u7ec4\u7684\u5143\u7d20\u662f\u5bf9\u8c61\uff0c\u5219 Arrays.equals() \u65b9\u6cd5\u53ef\u80fd\u4e0d\u8db3\u4ee5\u6bd4\u8f83\u5b83\u4eec\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u9010\u4e2a\u5143\u7d20\u5730\u6bd4\u8f83\u6570\u7ec4\uff1a<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>String[] arr1 = {\"a\", \"b\", \"c\"};\nString[] arr2 = {\"a\", \"b\", \"c\"};\n\nboolean isEqual = true;\nfor (int i = 0; i &lt; arr1.length; i++) {\n  if (!arr1[i].equals(arr2[i])) {\n    isEqual = false;\n    break;\n  }\n}\n\nif (isEqual) {\n  System.out.println(\"\u4e24\u4e2a\u6570\u7ec4\u76f8\u7b49\");\n} else {\n  System.out.println(\"\u4e24\u4e2a\u6570\u7ec4\u4e0d\u76f8\u7b49\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/strong><\/p>\n<p>\u4e5f\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u63d0\u4f9b\u4e86\u6bd4\u8f83\u6570\u7ec4\u7684\u529f\u80fd\u3002Guava \u5e93\u7684 Arrays.equivalence() \u65b9\u6cd5\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u6bd4\u8f83\u9009\u9879\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>import com.google.common.collect.Equivalence;\nimport com.google.common.collect.HashBasedEquivalence;\n\nEquivalence&lt;int[]&gt; equivalence = HashBasedEquivalence.create();\n\nboolean isEqual = equivalence.equivalent(arr1, arr2);\n\nif (isEqual) {\n  System.out.println(\"\u4e24\u4e2a\u6570\u7ec4\u76f8\u7b49\");\n} else {\n  System.out.println(\"\u4e24\u4e2a\u6570\u7ec4\u4e0d\u76f8\u7b49\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJava\u600e\u4e48\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u76f8\u7b49\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\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u662f\u5426\u76f8\u7b49\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a\u4f7f\u7528 arrays.equals() \u65b9\u6cd5\u8fdb\u884c\u9010\u4e2a\u5143\u7d20\u6bd4\u8f83\u5bf9\u5143\u7d20\u4e3a\u5bf9\u8c61\u7684\u6570\u7ec4\u8fdb\u884c\u9010\u4e2a\u5143\u7d20\u6bd4\u8f83\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\uff08\u5982 guava\uff09\u7684\u65b9\u6cd5\u8fdb\u884c\u6bd4\u8f83 \u5982\u4f55\u6bd4\u8f83\u4e24\u4e2a Java \u6570\u7ec4\u662f\u5426\u76f8\u7b49 \u5728 Java \u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u662f\u5426\u76f8\u7b49\uff1a 1. Arrays.equals() \u65b9\u6cd5 Arrays.equals() \u65b9\u6cd5\u662f\u6bd4\u8f83\u6570\u7ec4\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u3002\u5b83\u9010\u4e2a\u5143\u7d20\u5730\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\uff0c\u5982\u679c\u6240\u6709\u5143\u7d20\u90fd\u76f8\u7b49\uff0c\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u793a\u4f8b\uff1a int[] arr1 = {1, 2, 3}; int[] arr2 = {1, 2, 3}; boolean isEqual = Arrays.equals(arr1, arr2); if (isEqual) { System.out.println(&#8220;\u4e24\u4e2a\u6570\u7ec4\u76f8\u7b49&#8221;); } else { System.out.println(&#8220;\u4e24\u4e2a\u6570\u7ec4\u4e0d\u76f8\u7b49&#8221;); } \u767b\u5f55\u540e\u590d\u5236 2. \u9010\u4e2a\u5143\u7d20\u6bd4\u8f83 \u5982\u679c\u6570\u7ec4\u7684\u5143\u7d20\u662f\u5bf9\u8c61\uff0c\u5219 Arrays.equals() \u65b9\u6cd5\u53ef\u80fd\u4e0d\u8db3\u4ee5\u6bd4\u8f83\u5b83\u4eec\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u9010\u4e2a\u5143\u7d20\u5730\u6bd4\u8f83\u6570\u7ec4\uff1a \u793a\u4f8b\uff1a String[] [&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-39463","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39463","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=39463"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39463\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}