{"id":37283,"date":"2024-11-26T17:01:58","date_gmt":"2024-11-26T09:01:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37283\/"},"modified":"2024-11-26T17:01:58","modified_gmt":"2024-11-26T09:01:58","slug":"java%e4%b8%ad%e6%80%8e%e4%b9%88%e6%95%b0%e7%bb%84%e5%8e%bb%e9%87%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37283\/","title":{"rendered":"java\u4e2d\u600e\u4e48\u6570\u7ec4\u53bb\u91cd"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u53bb\u9664\u6570\u7ec4\u91cd\u590d\u5143\u7d20\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 set\uff0c\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u5143\u7d20\uff0c\u5e76\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f7f\u7528\u989d\u5916\u6570\u7ec4\uff0c\u9010\u4e2a\u904d\u5386\uff0c\u4e0d\u5728\u989d\u5916\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u52a0\u5165\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111303271650629.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u600e\u4e48\u6570\u7ec4\u53bb\u91cd\u63d2\u56fe\" alt=\"java\u4e2d\u600e\u4e48\u6570\u7ec4\u53bb\u91cd\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u53bb\u9664 Java \u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20<\/strong><\/p>\n<p>Java \u4e2d\u53bb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4ee5\u4e0b\u4ecb\u7ecd\u4e24\u79cd\u5e38\u89c1\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 Set<\/strong><\/p>\n<p>Set \u662f\u4e00\u79cd\u96c6\u5408\u7c7b\u578b\uff0c\u5b83\u4e0d\u5305\u542b\u91cd\u590d\u5143\u7d20\u3002\u901a\u8fc7\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a Set\uff0c\u53ef\u4ee5\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u5143\u7d20\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] arr = {1, 2, 3, 1, 4, 5};\n\n\/\/ \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a Set\nSet&lt;Integer&gt; set = new HashSet&lt;&gt;(Arrays.asList(arr));\n\n\/\/ \u5c06 Set \u8f6c\u6362\u4e3a\u6570\u7ec4\nint[] newArr = set.stream().mapToInt(Integer::intValue).toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u989d\u5916\u6570\u7ec4<\/strong><\/p>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u989d\u5916\u7684\u6570\u7ec4\u6765\u5b58\u50a8\u53bb\u91cd\u7684\u5143\u7d20\u3002\u5c06\u539f\u6570\u7ec4\u9010\u4e2a\u904d\u5386\uff0c\u5982\u679c\u5f53\u524d\u5143\u7d20\u4e0d\u5728\u989d\u5916\u6570\u7ec4\u4e2d\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u989d\u5916\u6570\u7ec4\u4e2d\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 1, 4, 5};\nint[] newArr = new int[arr.length];\nint idx = 0;\n\nfor (int i = 0; i &lt; arr.length; i++) {\n    boolean found = false;\n    for (int j = 0; j &lt; idx; j++) {\n        if (arr[i] == newArr[j]) {\n            found = true;\n            break;\n        }\n    }\n    if (!found) {\n        newArr[idx++] = arr[i];\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u6709\u6548\u5730\u53bb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u7684\u5143\u7d20\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u60c5\u51b5\uff0c\u4f8b\u5982\u6570\u7ec4\u5927\u5c0f\u3001\u6027\u80fd\u8981\u6c42\u7b49\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u600e\u4e48\u6570\u7ec4\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 \u4e2d\u53bb\u9664\u6570\u7ec4\u91cd\u590d\u5143\u7d20\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 set\uff0c\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u5143\u7d20\uff0c\u5e76\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f7f\u7528\u989d\u5916\u6570\u7ec4\uff0c\u9010\u4e2a\u904d\u5386\uff0c\u4e0d\u5728\u989d\u5916\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u52a0\u5165\u3002 \u5982\u4f55\u53bb\u9664 Java \u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20 Java \u4e2d\u53bb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4ee5\u4e0b\u4ecb\u7ecd\u4e24\u79cd\u5e38\u89c1\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 Set Set \u662f\u4e00\u79cd\u96c6\u5408\u7c7b\u578b\uff0c\u5b83\u4e0d\u5305\u542b\u91cd\u590d\u5143\u7d20\u3002\u901a\u8fc7\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a Set\uff0c\u53ef\u4ee5\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u5143\u7d20\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] arr = {1, 2, 3, 1, 4, 5}; \/\/ \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a Set Set&lt;Integer&gt; set = new HashSet&lt;&gt;(Arrays.asList(arr)); \/\/ \u5c06 Set \u8f6c\u6362\u4e3a\u6570\u7ec4 int[] newArr = set.stream().mapToInt(Integer::intValue).toArray(); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528\u989d\u5916\u6570\u7ec4 \u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u989d\u5916\u7684\u6570\u7ec4\u6765\u5b58\u50a8\u53bb\u91cd\u7684\u5143\u7d20\u3002\u5c06\u539f\u6570\u7ec4\u9010\u4e2a\u904d\u5386\uff0c\u5982\u679c\u5f53\u524d\u5143\u7d20\u4e0d\u5728\u989d\u5916\u6570\u7ec4\u4e2d\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u989d\u5916\u6570\u7ec4\u4e2d\uff1a int[] arr = {1, 2, 3, 1, 4, 5}; int[] newArr [&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-37283","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37283","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=37283"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37283\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}