{"id":39677,"date":"2024-11-26T13:32:14","date_gmt":"2024-11-26T05:32:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39677\/"},"modified":"2024-11-26T13:32:14","modified_gmt":"2024-11-26T05:32:14","slug":"%e4%b8%80%e7%bb%b4%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%8e%bb%e9%99%a4%e9%87%8d%e5%a4%8d%e5%85%83%e7%b4%a0java","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39677\/","title":{"rendered":"\u4e00\u7ef4\u6570\u7ec4\u600e\u4e48\u53bb\u9664\u91cd\u590d\u5143\u7d20java"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u53bb\u9664\u4e00\u7ef4\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a\u521b\u5efa\u4e00\u4e2a set\uff08\u5982 hashset\uff09\u6765\u5b58\u50a8\u552f\u4e00\u5143\u7d20\u3002\u904d\u5386\u6570\u7ec4\uff0c\u5c06\u5143\u7d20\u9010\u4e2a\u6dfb\u52a0\u5230 set \u4e2d\u3002\u6839\u636e set \u7684\u5927\u5c0f\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5e76\u904d\u5386 set\uff0c\u5c06\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111621071954749.jpg\" class=\"aligncenter\" title=\"\u4e00\u7ef4\u6570\u7ec4\u600e\u4e48\u53bb\u9664\u91cd\u590d\u5143\u7d20java\u63d2\u56fe\" alt=\"\u4e00\u7ef4\u6570\u7ec4\u600e\u4e48\u53bb\u9664\u91cd\u590d\u5143\u7d20java\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u53bb\u9664\u4e00\u7ef4\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20<\/strong><\/p>\n<p>\u5728 Java \u4e2d\u53bb\u9664\u4e00\u7ef4\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5b9e\u73b0\u5b83\u7684\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 Set \u5b58\u50a8\u552f\u4e00\u5143\u7d20<\/strong><\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Set\uff0c\u4f8b\u5982 HashSet\u3002<\/li>\n<li>\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u9010\u4e2a\u6dfb\u52a0\u5143\u7d20\u5230 Set \u4e2d\u3002<\/li>\n<li>Set \u7684\u7279\u6027\u786e\u4fdd\u5b83\u53ea\u5b58\u50a8\u552f\u4e00\u7684\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><strong>2. \u5c06 Set \u590d\u5236\u5230\u65b0\u6570\u7ec4<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u957f\u5ea6\u4e0e Set \u76f8\u540c\u3002<\/li>\n<li>\u904d\u5386 Set\uff0c\u5e76\u5c06\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>import java.util.Arrays;\nimport java.util.HashSet;\n\npublic class RemoveDuplicates {\n\n    public static void main(String[] args) {\n        int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};\n\n        \/\/ \u4f7f\u7528 Set \u5b58\u50a8\u552f\u4e00\u5143\u7d20\n        Set&lt;Integer&gt; uniqueElements = new HashSet&lt;&gt;();\n        for (int element : arr) {\n            uniqueElements.add(element);\n        }\n\n        \/\/ \u5c06 Set \u590d\u5236\u5230\u65b0\u6570\u7ec4\n        int[] newArr = new int[uniqueElements.size()];\n        int index = 0;\n        for (Integer element : uniqueElements) {\n            newArr[index++] = element;\n        }\n\n        \/\/ \u6253\u5370\u65b0\u6570\u7ec4\n        System.out.println(Arrays.toString(newArr));\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>[1, 2, 3, 4, 5]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e00\u7ef4\u6570\u7ec4\u600e\u4e48\u53bb\u9664\u91cd\u590d\u5143\u7d20java\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\uff0c\u53bb\u9664\u4e00\u7ef4\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a\u521b\u5efa\u4e00\u4e2a set\uff08\u5982 hashset\uff09\u6765\u5b58\u50a8\u552f\u4e00\u5143\u7d20\u3002\u904d\u5386\u6570\u7ec4\uff0c\u5c06\u5143\u7d20\u9010\u4e2a\u6dfb\u52a0\u5230 set \u4e2d\u3002\u6839\u636e set \u7684\u5927\u5c0f\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5e76\u904d\u5386 set\uff0c\u5c06\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002 \u5982\u4f55\u4f7f\u7528 Java \u53bb\u9664\u4e00\u7ef4\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20 \u5728 Java \u4e2d\u53bb\u9664\u4e00\u7ef4\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5b9e\u73b0\u5b83\u7684\u6b65\u9aa4\uff1a 1. \u4f7f\u7528 Set \u5b58\u50a8\u552f\u4e00\u5143\u7d20 \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Set\uff0c\u4f8b\u5982 HashSet\u3002 \u904d\u5386\u6570\u7ec4\uff0c\u5e76\u9010\u4e2a\u6dfb\u52a0\u5143\u7d20\u5230 Set \u4e2d\u3002 Set \u7684\u7279\u6027\u786e\u4fdd\u5b83\u53ea\u5b58\u50a8\u552f\u4e00\u7684\u5143\u7d20\u3002 2. \u5c06 Set \u590d\u5236\u5230\u65b0\u6570\u7ec4 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u957f\u5ea6\u4e0e Set \u76f8\u540c\u3002 \u904d\u5386 Set\uff0c\u5e76\u5c06\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002 \u793a\u4f8b\u4ee3\u7801\uff1a import java.util.Arrays; import java.util.HashSet; public class RemoveDuplicates { public static void main(String[] args) { int[] arr [&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-39677","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39677","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=39677"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39677\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}