{"id":40012,"date":"2024-11-26T12:21:19","date_gmt":"2024-11-26T04:21:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40012\/"},"modified":"2024-11-26T12:21:19","modified_gmt":"2024-11-26T04:21:19","slug":"java%e9%87%8c%e4%b8%a4%e4%b8%aa%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e8%8e%b7%e5%8f%96%e5%b9%b6%e9%9b%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/40012\/","title":{"rendered":"java\u91cc\u4e24\u4e2a\u6570\u7ec4\u600e\u4e48\u83b7\u53d6\u5e76\u96c6"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u83b7\u53d6\u4e24\u4e2a\u6570\u7ec4\u7684\u5e76\u96c6\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a1. \u5408\u5e76\u4e24\u4e2a\u6570\u7ec4\uff1b2. \u4f7f\u7528 set \u53bb\u91cd\uff1b3. \u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u793a\u4f8b\u4ee3\u7801\uff1a\u4f7f\u7528 arrays.copyof() \u5408\u5e76\u6570\u7ec4\uff0cset.add() \u53bb\u91cd\uff0c\u5e76\u901a\u8fc7 stream().maptoint(integer::intvalue).toarray() \u5c06 set \u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111718162552509.jpg\" class=\"aligncenter\" title=\"java\u91cc\u4e24\u4e2a\u6570\u7ec4\u600e\u4e48\u83b7\u53d6\u5e76\u96c6\u63d2\u56fe\" alt=\"java\u91cc\u4e24\u4e2a\u6570\u7ec4\u600e\u4e48\u83b7\u53d6\u5e76\u96c6\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u83b7\u53d6\u6570\u7ec4\u5e76\u96c6<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong>\u5982\u4f55\u83b7\u53d6 Java \u4e2d\u4e24\u4e2a\u6570\u7ec4\u7684\u5e76\u96c6\uff1f<\/p>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<p>Java \u4e2d\u83b7\u53d6\u6570\u7ec4\u5e76\u96c6\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li> <strong>\u5408\u5e76\u4e24\u4e2a\u6570\u7ec4\uff1a<\/strong>\u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5\u5c06\u4e24\u4e2a\u6570\u7ec4\u5408\u5e76\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002<\/li>\n<li> <strong>\u53bb\u91cd\uff1a<\/strong>\u5bf9\u5408\u5e76\u540e\u7684\u6570\u7ec4\u4f7f\u7528 Set.add() \u65b9\u6cd5\u53bb\u91cd\uff0c\u5c06\u91cd\u590d\u5143\u7d20\u6392\u9664\u3002<\/li>\n<li> <strong>\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a<\/strong>\u5c06\u53bb\u91cd\u540e\u7684 Set \u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u8fd4\u56de\u5e76\u96c6\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>import java.util.Arrays;\nimport java.util.Set;\nimport java.util.HashSet;\n\npublic class ArrayUnion {\n\n    public static int[] union(int[] arr1, int[] arr2) {\n        \/\/ \u5408\u5e76\u6570\u7ec4\n        int[] mergedArray = Arrays.copyOf(arr1, arr1.length + arr2.length);\n\n        for (int i = 0; i &lt; arr2.length; i++) {\n            mergedArray[arr1.length + i] = arr2[i];\n        }\n\n        \/\/ \u53bb\u91cd\n        Set&lt;Integer&gt; uniqueElements = new HashSet&lt;&gt;();\n\n        for (int num : mergedArray) {\n            uniqueElements.add(num);\n        }\n\n        \/\/ \u8f6c\u6362\u4e3a\u6570\u7ec4\n        return uniqueElements.stream().mapToInt(Integer::intValue).toArray();\n    }\n\n    public static void main(String[] args) {\n        int[] arr1 = {1, 2, 3, 4, 5};\n        int[] arr2 = {3, 4, 5, 6, 7};\n\n        int[] result = union(arr1, arr2);\n        System.out.println(Arrays.toString(result)); \/\/ \u8f93\u51fa\uff1a [1, 2, 3, 4, 5, 6, 7]\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u91cc\u4e24\u4e2a\u6570\u7ec4\u600e\u4e48\u83b7\u53d6\u5e76\u96c6\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>java \u4e2d\u83b7\u53d6\u4e24\u4e2a\u6570\u7ec4\u7684\u5e76\u96c6\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a1. \u5408\u5e76\u4e24\u4e2a\u6570\u7ec4\uff1b2. \u4f7f\u7528 set \u53bb\u91cd\uff1b3. \u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u793a\u4f8b\u4ee3\u7801\uff1a\u4f7f\u7528 arrays.copyof() \u5408\u5e76\u6570\u7ec4\uff0cset.add() \u53bb\u91cd\uff0c\u5e76\u901a\u8fc7 stream().maptoint(integer::intvalue).toarray() \u5c06 set \u8f6c\u6362\u4e3a\u6570\u7ec4\u3002 Java \u4e2d\u83b7\u53d6\u6570\u7ec4\u5e76\u96c6 \u95ee\u9898\uff1a\u5982\u4f55\u83b7\u53d6 Java \u4e2d\u4e24\u4e2a\u6570\u7ec4\u7684\u5e76\u96c6\uff1f \u89e3\u7b54\uff1a Java \u4e2d\u83b7\u53d6\u6570\u7ec4\u5e76\u96c6\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5408\u5e76\u4e24\u4e2a\u6570\u7ec4\uff1a\u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5\u5c06\u4e24\u4e2a\u6570\u7ec4\u5408\u5e76\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002 \u53bb\u91cd\uff1a\u5bf9\u5408\u5e76\u540e\u7684\u6570\u7ec4\u4f7f\u7528 Set.add() \u65b9\u6cd5\u53bb\u91cd\uff0c\u5c06\u91cd\u590d\u5143\u7d20\u6392\u9664\u3002 \u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a\u5c06\u53bb\u91cd\u540e\u7684 Set \u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u8fd4\u56de\u5e76\u96c6\u3002 \u793a\u4f8b\u4ee3\u7801\uff1a import java.util.Arrays; import java.util.Set; import java.util.HashSet; public class ArrayUnion { public static int[] union(int[] arr1, int[] arr2) { \/\/ \u5408\u5e76\u6570\u7ec4 int[] mergedArray [&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-40012","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40012","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=40012"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40012\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}