{"id":65891,"date":"2025-05-03T15:23:54","date_gmt":"2025-05-03T07:23:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65891\/"},"modified":"2025-05-03T15:23:54","modified_gmt":"2025-05-03T07:23:54","slug":"java%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%88%a0%e9%99%a4%e7%a9%ba%e7%9a%84%e6%95%b0%e6%8d%ae-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65891\/","title":{"rendered":"java\u6570\u7ec4\u600e\u4e48\u5220\u9664\u7a7a\u7684\u6570\u636e"},"content":{"rendered":"<blockquote><p>\n  java \u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u6570\u636e\u6709\u56db\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 arrays.stream() \u548c filter() \u8fc7\u6ee4\u51fa\u975e\u7a7a\u5143\u7d20\u3002\u4f7f\u7528 list.of() \u548c removeall() \u5c06\u7a7a\u5143\u7d20\u6dfb\u52a0\u5230\u65b0\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4ece\u539f\u59cb\u5217\u8868\u4e2d\u5220\u9664\u3002\u4f7f\u7528 stream.concat() \u548c stream.of() \u5c06\u7a7a\u5143\u7d20\u6d41\u8fde\u63a5\u5230\u539f\u59cb\u6570\u7ec4\u6d41\uff0c\u518d\u8fc7\u6ee4\u51fa\u975e\u7a7a\u5143\u7d20\u3002\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u5c06\u975e\u7a7a\u5143\u7d20\u6dfb\u52a0\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111314363568869.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u600e\u4e48\u5220\u9664\u7a7a\u7684\u6570\u636e\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u600e\u4e48\u5220\u9664\u7a7a\u7684\u6570\u636e\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u6570\u636e<\/strong><\/p>\n<p>\u5728 Java \u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u6570\u636e\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u4ee5\u4e0b\u662f\u5176\u4e2d\u4e00\u4e9b\u5e38\u7528\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 Arrays.stream() \u548c filter()<\/strong><\/p>\n<pre>int[] arr = {1, 2, 0, 4, 5, 0, 7};\n\n\/\/ \u4f7f\u7528 Arrays.stream() \u521b\u5efa\u6d41\nIntStream stream = Arrays.stream(arr);\n\n\/\/ \u4f7f\u7528 filter() \u8fc7\u6ee4\u51fa\u4e0d\u4e3a 0 \u7684\u5143\u7d20\nIntStream filteredStream = stream.filter(n -&gt; n != 0);\n\n\/\/ \u5c06\u8fc7\u6ee4\u540e\u7684\u6d41\u8f6c\u6362\u4e3a\u6570\u7ec4\nint[] newArr = filteredStream.toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 List.of() \u548c removeAll()<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>List&lt;Integer&gt; list = List.of(1, 2, 0, 4, 5, 0, 7);\n\n\/\/ \u5c06\u5217\u8868\u4e2d\u7684\u6240\u6709\u7a7a\u6570\u636e (0) \u6dfb\u52a0\u5230\u65b0\u5217\u8868\nList&lt;Integer&gt; zeros = List.of(0);\n\n\/\/ \u4ece\u5217\u8868\u4e2d\u5220\u9664 newZeros \u4e2d\u7684\u6240\u6709\u5143\u7d20\nlist.removeAll(zeros);\n\n\/\/ \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\nint[] newArr = list.stream().mapToInt(i -&gt; i).toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Stream.concat() \u548c Stream.of()<\/strong><\/p>\n<pre>int[] arr = {1, 2, 0, 4, 5, 0, 7};\n\n\/\/ \u4e3a\u6240\u6709\u7a7a\u6570\u636e (0) \u521b\u5efa\u4e00\u4e2a\u5305\u542b 0 \u7684\u6d41\nStream&lt;Integer&gt; zeroStream = Stream.of(0);\n\n\/\/ \u5c06\u4e24\u4e2a\u6d41\u8fde\u63a5\u5728\u4e00\u8d77\nStream&lt;Integer&gt; combinedStream = Stream.concat(Arrays.stream(arr), zeroStream);\n\n\/\/ \u8fc7\u6ee4\u51fa\u4e0d\u4e3a 0 \u7684\u5143\u7d20\nStream&lt;Integer&gt; filteredStream = combinedStream.filter(n -&gt; n != 0);\n\n\/\/ \u5c06\u8fc7\u6ee4\u540e\u7684\u6d41\u8f6c\u6362\u4e3a\u6570\u7ec4\nint[] newArr = filteredStream.toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u5faa\u73af<\/strong><\/p>\n<pre>int[] arr = {1, 2, 0, 4, 5, 0, 7};\nint[] newArr = new int[arr.length];\nint index = 0;\n\n\/\/ \u904d\u5386\u6570\u7ec4\nfor (int n : arr) {\n    \/\/ \u5982\u679c n \u4e0d\u4e3a 0\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230 newZeros\n    if (n != 0) {\n        newArr[index++] = n;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u600e\u4e48\u5220\u9664\u7a7a\u7684\u6570\u636e\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u6570\u636e\u6709\u56db\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 arrays.stream() \u548c filter() \u8fc7\u6ee4\u51fa\u975e\u7a7a\u5143\u7d20\u3002\u4f7f\u7528 list.of() \u548c removeall() \u5c06\u7a7a\u5143\u7d20\u6dfb\u52a0\u5230\u65b0\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4ece\u539f\u59cb\u5217\u8868\u4e2d\u5220\u9664\u3002\u4f7f\u7528 stream.concat() \u548c stream.of() \u5c06\u7a7a\u5143\u7d20\u6d41\u8fde\u63a5\u5230\u539f\u59cb\u6570\u7ec4\u6d41\uff0c\u518d\u8fc7\u6ee4\u51fa\u975e\u7a7a\u5143\u7d20\u3002\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u5c06\u975e\u7a7a\u5143\u7d20\u6dfb\u52a0\u5230\u65b0\u6570\u7ec4\u4e2d\u3002 Java \u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u6570\u636e \u5728 Java \u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u6570\u636e\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u4ee5\u4e0b\u662f\u5176\u4e2d\u4e00\u4e9b\u5e38\u7528\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 Arrays.stream() \u548c filter() int[] arr = {1, 2, 0, 4, 5, 0, 7}; \/\/ \u4f7f\u7528 Arrays.stream() \u521b\u5efa\u6d41 IntStream stream = Arrays.stream(arr); \/\/ \u4f7f\u7528 filter() \u8fc7\u6ee4\u51fa\u4e0d\u4e3a 0 \u7684\u5143\u7d20 IntStream filteredStream = stream.filter(n -&gt; n != [&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-65891","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65891","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=65891"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65891\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}