{"id":39101,"date":"2024-11-26T16:42:25","date_gmt":"2024-11-26T08:42:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39101\/"},"modified":"2024-11-26T16:42:25","modified_gmt":"2024-11-26T08:42:25","slug":"java%e6%95%b0%e7%bb%84%e9%87%8c%e7%9a%84%e5%85%83%e7%b4%a0%e6%80%8e%e4%b9%88%e5%8e%bb%e9%87%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39101\/","title":{"rendered":"java\u6570\u7ec4\u91cc\u7684\u5143\u7d20\u600e\u4e48\u53bb\u91cd"},"content":{"rendered":"<blockquote><p>\n  java\u6570\u7ec4\u53bb\u91cd\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528hashset\uff1a\u5c06\u6570\u7ec4\u5143\u7d20\u653e\u5165hashset\u4e2d\uff0c\u518d\u5c06\u53bb\u91cd\u540e\u7684\u5143\u7d20\u590d\u5236\u56de\u6570\u7ec4\u3002\u4f7f\u7528linkedhashset\uff1a\u4e0ehashset\u7c7b\u4f3c\uff0c\u4f46\u4fdd\u7559\u5143\u7d20\u63d2\u5165\u987a\u5e8f\u3002\u4f7f\u7528arrays.sort()\u548c\u53cc\u6307\u9488\uff1a\u6392\u5e8f\u6570\u7ec4\uff0c\u8df3\u8fc7\u76f8\u90bb\u91cd\u590d\u5143\u7d20\uff0c\u5c06\u53bb\u91cd\u540e\u7684\u5143\u7d20\u590d\u5236\u56de\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111604032012536.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u91cc\u7684\u5143\u7d20\u600e\u4e48\u53bb\u91cd\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u91cc\u7684\u5143\u7d20\u600e\u4e48\u53bb\u91cd\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\u53bb\u91cd<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong>\u5982\u4f55\u4ece Java \u6570\u7ec4\u4e2d\u5220\u9664\u91cd\u590d\u5143\u7d20\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong><\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 HashSet<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>HashSet \u662f\u4e00\u4e2a\u65e0\u5e8f\u96c6\u5408\u7c7b\uff0c\u5b83\u4fdd\u8bc1\u5143\u7d20\u4e0d\u91cd\u590d\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u5143\u7d20\u6dfb\u52a0\u5230 HashSet \u4e2d\uff0c\u7136\u540e\u518d\u5c06\u53bb\u91cd\u540e\u7684\u5143\u7d20\u590d\u5236\u56de\u6570\u7ec4\u3002<\/p>\n<pre>Set&lt;Integer&gt; uniqueSet = new HashSet&lt;&gt;();\nfor (int num : array) {\n    uniqueSet.add(num);\n}\nint[] uniqueArray = new int[uniqueSet.size()];\nint index = 0;\nfor (int num : uniqueSet) {\n    uniqueArray[index++] = num;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 LinkedHashSet<\/strong><\/p>\n<p>LinkedHashSet \u662f\u4e00\u4e2a\u6709\u5e8f\u96c6\u5408\u7c7b\uff0c\u5b83\u4e5f\u4fdd\u8bc1\u5143\u7d20\u4e0d\u91cd\u590d\u3002\u4e0e HashSet \u76f8\u6bd4\uff0cLinkedHashSet \u4fdd\u7559\u5143\u7d20\u63d2\u5165\u65f6\u7684\u987a\u5e8f\u3002<\/p>\n<pre>Set&lt;Integer&gt; uniqueSet = new LinkedHashSet&lt;&gt;();\nfor (int num : array) {\n    uniqueSet.add(num);\n}\nint[] uniqueArray = new int[uniqueSet.size()];\nint index = 0;\nfor (int num : uniqueSet) {\n    uniqueArray[index++] = num;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 3\uff1a\u4f7f\u7528 Arrays.sort() \u548c\u53cc\u6307\u9488<\/strong><\/p>\n<ol>\n<li>\u4f7f\u7528 Arrays.sort() \u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<li>\u4f7f\u7528\u4e24\u4e2a\u6307\u9488\u904d\u5386\u6392\u5e8f\u540e\u7684\u6570\u7ec4\uff0c\u5982\u679c\u76f8\u90bb\u5143\u7d20\u76f8\u540c\uff0c\u5219\u8df3\u8fc7\u91cd\u590d\u7684\u5143\u7d20\u3002<\/li>\n<li>\u5c06\u53bb\u91cd\u540e\u7684\u5143\u7d20\u590d\u5236\u56de\u6570\u7ec4\u3002<\/li>\n<\/ol>\n<pre>Arrays.sort(array);\nint index = 0;\nfor (int i = 1; i &lt; array.length; i++) {\n    if (array[i] != array[index]) {\n        array[++index] = array[i];\n    }\n}\nint[] uniqueArray = new int[index + 1];\nSystem.arraycopy(array, 0, uniqueArray, 0, index + 1);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u91cc\u7684\u5143\u7d20\u600e\u4e48\u53bb\u91cd\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\u6570\u7ec4\u53bb\u91cd\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528hashset\uff1a\u5c06\u6570\u7ec4\u5143\u7d20\u653e\u5165hashset\u4e2d\uff0c\u518d\u5c06\u53bb\u91cd\u540e\u7684\u5143\u7d20\u590d\u5236\u56de\u6570\u7ec4\u3002\u4f7f\u7528linkedhashset\uff1a\u4e0ehashset\u7c7b\u4f3c\uff0c\u4f46\u4fdd\u7559\u5143\u7d20\u63d2\u5165\u987a\u5e8f\u3002\u4f7f\u7528arrays.sort()\u548c\u53cc\u6307\u9488\uff1a\u6392\u5e8f\u6570\u7ec4\uff0c\u8df3\u8fc7\u76f8\u90bb\u91cd\u590d\u5143\u7d20\uff0c\u5c06\u53bb\u91cd\u540e\u7684\u5143\u7d20\u590d\u5236\u56de\u6570\u7ec4\u3002 Java \u6570\u7ec4\u53bb\u91cd \u95ee\u9898\uff1a\u5982\u4f55\u4ece Java \u6570\u7ec4\u4e2d\u5220\u9664\u91cd\u590d\u5143\u7d20\uff1f \u56de\u7b54\uff1a \u65b9\u6cd5 1\uff1a\u4f7f\u7528 HashSet \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b HashSet \u662f\u4e00\u4e2a\u65e0\u5e8f\u96c6\u5408\u7c7b\uff0c\u5b83\u4fdd\u8bc1\u5143\u7d20\u4e0d\u91cd\u590d\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u5143\u7d20\u6dfb\u52a0\u5230 HashSet \u4e2d\uff0c\u7136\u540e\u518d\u5c06\u53bb\u91cd\u540e\u7684\u5143\u7d20\u590d\u5236\u56de\u6570\u7ec4\u3002 Set&lt;Integer&gt; uniqueSet = new HashSet&lt;&gt;(); for (int num : array) { uniqueSet.add(num); } int[] uniqueArray = new int[uniqueSet.size()]; int index = 0; for (int num : uniqueSet) { uniqueArray[index++] = num; } \u767b\u5f55\u540e\u590d\u5236 \u65b9\u6cd5 2\uff1a\u4f7f\u7528 LinkedHashSet LinkedHashSet \u662f\u4e00\u4e2a\u6709\u5e8f\u96c6\u5408\u7c7b\uff0c\u5b83\u4e5f\u4fdd\u8bc1\u5143\u7d20\u4e0d\u91cd\u590d\u3002\u4e0e HashSet \u76f8\u6bd4\uff0cLinkedHashSet [&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-39101","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39101","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=39101"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39101\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}