{"id":36923,"date":"2024-11-26T12:41:57","date_gmt":"2024-11-26T04:41:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36923\/"},"modified":"2024-11-26T12:41:57","modified_gmt":"2024-11-26T04:41:57","slug":"java-%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%8e%bb%e9%87%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36923\/","title":{"rendered":"java \u6570\u7ec4\u600e\u4e48\u53bb\u91cd"},"content":{"rendered":"<blockquote><p>\n  java\u6570\u7ec4\u53bb\u91cd\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528hashset\uff1a\u5c06\u6570\u7ec4\u5143\u7d20\u6dfb\u52a0\u5230hashset\u4e2d\u53bb\u91cd\uff0c\u518d\u8f6c\u4e3a\u6570\u7ec4\u3002\u4f7f\u7528\u6392\u5e8f\u548c\u53cc\u6307\u9488\uff1a\u5bf9\u6570\u7ec4\u6392\u5e8f\uff0c\u4f7f\u7528\u53cc\u6307\u9488\u4ece\u4e24\u7aef\u5411\u4e2d\u95f4\u904d\u5386\uff0c\u8df3\u8fc7\u91cd\u590d\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111221425985079.jpg\" class=\"aligncenter\" title=\"java \u6570\u7ec4\u600e\u4e48\u53bb\u91cd\u63d2\u56fe\" alt=\"java \u6570\u7ec4\u600e\u4e48\u53bb\u91cd\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\u5982\u4f55\u53bb\u91cd<\/strong><\/p>\n<p>\u6570\u7ec4\u53bb\u91cd\u64cd\u4f5c\u662f\u6307\u4ece\u6570\u7ec4\u4e2d\u79fb\u9664\u91cd\u590d\u5143\u7d20\uff0c\u4ece\u800c\u5f97\u5230\u4e0d\u91cd\u590d\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002\u5728 Java \u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6570\u7ec4\u53bb\u91cd\uff0c\u4ee5\u4e0b\u4ecb\u7ecd\u4e24\u79cd\u5e38\u89c1\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u4f7f\u7528 HashSet<\/strong><\/p>\n<p>HashSet\u662f\u4e00\u79cd\u96c6\u5408\u7c7b\uff0c\u5b83\u81ea\u52a8\u4f1a\u53bb\u91cd\u5143\u7d20\u3002\u53ef\u4ee5\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6dfb\u52a0\u5230 HashSet \u4e2d\uff0c\u7136\u540e\u5c06 HashSet \u8f6c\u5316\u4e3a\u6570\u7ec4\u5373\u53ef\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};\n\n\/\/ \u4f7f\u7528 HashSet \u53bb\u91cd\nSet&lt;Integer&gt; set = new HashSet&lt;&gt;();\nfor (int num : arr) {\n    set.add(num);\n}\n\n\/\/ \u5c06 HashSet \u8f6c\u4e3a\u6570\u7ec4\nint[] result = new int[set.size()];\nint index = 0;\nfor (int num : set) {\n    result[index++] = num;\n}\n\nSystem.out.println(Arrays.toString(result)); \/\/ \u8f93\u51fa\uff1a[1, 2, 3, 4, 5]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u6392\u5e8f\u548c\u53cc\u6307\u9488<\/strong><\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u662f\u5148\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u4f7f\u7528\u4e24\u4e2a\u6307\u9488\u4ece\u6570\u7ec4\u4e24\u7aef\u5411\u4e2d\u95f4\u904d\u5386\u3002\u5982\u679c\u9047\u5230\u91cd\u590d\u5143\u7d20\uff0c\u5219\u8df3\u8fc7\u91cd\u590d\u7684\u5143\u7d20\u3002<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};\n\n\/\/ \u6392\u5e8f\u6570\u7ec4\nArrays.sort(arr);\n\n\/\/ \u4f7f\u7528\u53cc\u6307\u9488\u53bb\u91cd\nint[] result = new int[arr.length];\nint index = 0;\nint left = 0;\nint right = 1;\n\nwhile (right &lt; arr.length) {\n    if (arr[left] != arr[right]) {\n        result[index++] = arr[left];\n        left = right;\n    }\n    right++;\n}\n\n\/\/ \u5c06\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u7ed3\u679c\u4e2d\nresult[index++] = arr[left];\n\nSystem.out.println(Arrays.toString(result)); \/\/ \u8f93\u51fa\uff1a[1, 2, 3, 4, 5]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava \u6570\u7ec4\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\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528hashset\uff1a\u5c06\u6570\u7ec4\u5143\u7d20\u6dfb\u52a0\u5230hashset\u4e2d\u53bb\u91cd\uff0c\u518d\u8f6c\u4e3a\u6570\u7ec4\u3002\u4f7f\u7528\u6392\u5e8f\u548c\u53cc\u6307\u9488\uff1a\u5bf9\u6570\u7ec4\u6392\u5e8f\uff0c\u4f7f\u7528\u53cc\u6307\u9488\u4ece\u4e24\u7aef\u5411\u4e2d\u95f4\u904d\u5386\uff0c\u8df3\u8fc7\u91cd\u590d\u5143\u7d20\u3002 Java \u6570\u7ec4\u5982\u4f55\u53bb\u91cd \u6570\u7ec4\u53bb\u91cd\u64cd\u4f5c\u662f\u6307\u4ece\u6570\u7ec4\u4e2d\u79fb\u9664\u91cd\u590d\u5143\u7d20\uff0c\u4ece\u800c\u5f97\u5230\u4e0d\u91cd\u590d\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002\u5728 Java \u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6570\u7ec4\u53bb\u91cd\uff0c\u4ee5\u4e0b\u4ecb\u7ecd\u4e24\u79cd\u5e38\u89c1\u65b9\u6cd5\u3002 \u4f7f\u7528 HashSet HashSet\u662f\u4e00\u79cd\u96c6\u5408\u7c7b\uff0c\u5b83\u81ea\u52a8\u4f1a\u53bb\u91cd\u5143\u7d20\u3002\u53ef\u4ee5\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6dfb\u52a0\u5230 HashSet \u4e2d\uff0c\u7136\u540e\u5c06 HashSet \u8f6c\u5316\u4e3a\u6570\u7ec4\u5373\u53ef\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] arr = {1, 2, 3, 4, 5, 1, 2, 3}; \/\/ \u4f7f\u7528 HashSet \u53bb\u91cd Set&lt;Integer&gt; set = new HashSet&lt;&gt;(); for (int num : arr) { set.add(num); } \/\/ \u5c06 HashSet \u8f6c\u4e3a\u6570\u7ec4 int[] result = new int[set.size()]; int index = 0; [&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-36923","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36923","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=36923"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36923\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}