{"id":37912,"date":"2024-11-26T12:09:24","date_gmt":"2024-11-26T04:09:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37912\/"},"modified":"2024-11-26T12:09:24","modified_gmt":"2024-11-26T04:09:24","slug":"java%e6%80%8e%e4%b9%88%e5%be%80%e6%95%b0%e7%bb%84%e5%a2%9e%e5%8a%a0%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37912\/","title":{"rendered":"java\u600e\u4e48\u5f80\u6570\u7ec4\u589e\u52a0\u5143\u7d20"},"content":{"rendered":"<blockquote><p>\n  \u5f80 java \u6570\u7ec4\u4e2d\u589e\u52a0\u5143\u7d20\u7684\u65b9\u6cd5\uff1a\u6269\u5bb9\u5e76\u590d\u5236\u539f\u6709\u5143\u7d20\u3002\u4f7f\u7528 arraylist\uff08\u52a8\u6001\u6570\u7ec4\uff09\u3002\u4f7f\u7528 guava \u5e93\u7684 collections2.addifabsent() \u65b9\u6cd5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111312512646770.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5f80\u6570\u7ec4\u589e\u52a0\u5143\u7d20\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5f80\u6570\u7ec4\u589e\u52a0\u5143\u7d20\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5f80 Java \u6570\u7ec4\u4e2d\u589e\u52a0\u5143\u7d20<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u6570\u7ec4\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u56fa\u5b9a\u957f\u5ea6\u7684\u76f8\u540c\u6570\u636e\u7c7b\u578b\u5143\u7d20\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6570\u7ec4\u521b\u5efa\u540e\u5176\u5927\u5c0f\u4e0d\u53ef\u6539\u53d8\u3002\u7136\u800c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u65b0\u5143\u7d20\uff1a<\/p>\n<p><strong>1. \u6269\u5bb9\u5e76\u590d\u5236\u539f\u6709\u5143\u7d20<\/strong><\/p>\n<p>\u4e3a\u4e86\u5411\u73b0\u6709\u6570\u7ec4\u4e2d\u6dfb\u52a0\u66f4\u591a\u5143\u7d20\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u3001\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6709\u5143\u7d20\u590d\u5236\u5230\u5176\u4e2d\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] originalArray = {1, 2, 3};\nint[] newArray = new int[originalArray.length + 1];\n\nfor (int i = 0; i &lt; originalArray.length; i++) {\n    newArray[i] = originalArray[i];\n}\nnewArray[originalArray.length] = 4; \/\/ \u6dfb\u52a0\u65b0\u5143\u7d20\n\n\/\/ \u66f4\u65b0\u539f\u6709\u6570\u7ec4\u7684\u5f15\u7528\noriginalArray = newArray;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 ArrayList<\/strong><\/p>\n<p>ArrayList \u662f\u4e00\u4e2a\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u52a8\u8c03\u6574\u5927\u5c0f\u3002\u5b83\u6bd4\u6269\u5bb9\u5e76\u590d\u5236\u539f\u6709\u5143\u7d20\u7684\u65b9\u6cd5\u66f4\u7b80\u5355\u4fbf\u6377\u3002<\/p>\n<pre>ArrayList&lt;Integer&gt; originalList = new ArrayList&lt;&gt;(Arrays.asList(1, 2, 3));\noriginalList.add(4); \/\/ \u6dfb\u52a0\u65b0\u5143\u7d20<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Guava \u5e93<\/strong><\/p>\n<p>Guava \u5e93\u63d0\u4f9b\u4e86 Collections2.addIfAbsent() \u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20\uff0c\u5982\u679c\u8be5\u5143\u7d20\u4e0d\u5b58\u5728\u5219\u4e0d\u4f1a\u6dfb\u52a0\u3002<\/p>\n<pre>import com.google.common.collect.Collections2;\n\nint[] originalArray = {1, 2, 3};\nCollections2.addIfAbsent(originalArray, 4); \/\/ \u4ec5\u5728\u4e0d\u5b58\u5728\u65f6\u6dfb\u52a0\u65b0\u5143\u7d20<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u7ec4\u7684\u9884\u671f\u5927\u5c0f\u548c\u6dfb\u52a0\u5143\u7d20\u7684\u9891\u7387\u3002\u5bf9\u4e8e\u5c0f\u578b\u6570\u7ec4\u6216\u5076\u5c14\u6dfb\u52a0\u5143\u7d20\u7684\u60c5\u51b5\uff0c\u6269\u5bb9\u5e76\u590d\u5236\u539f\u6709\u5143\u7d20\u7684\u65b9\u6cd5\u5c31\u8db3\u591f\u4e86\u3002\u5bf9\u4e8e\u9700\u8981\u9891\u7e41\u6dfb\u52a0\u5143\u7d20\u7684\u5927\u578b\u6570\u7ec4\uff0c\u4f7f\u7528 ArrayList \u6216 Guava \u5e93\u4f1a\u66f4\u6709\u6548\u7387\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5f80\u6570\u7ec4\u589e\u52a0\u5143\u7d20\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>\u5f80 java \u6570\u7ec4\u4e2d\u589e\u52a0\u5143\u7d20\u7684\u65b9\u6cd5\uff1a\u6269\u5bb9\u5e76\u590d\u5236\u539f\u6709\u5143\u7d20\u3002\u4f7f\u7528 arraylist\uff08\u52a8\u6001\u6570\u7ec4\uff09\u3002\u4f7f\u7528 guava \u5e93\u7684 collections2.addifabsent() \u65b9\u6cd5\u3002 \u5982\u4f55\u5f80 Java \u6570\u7ec4\u4e2d\u589e\u52a0\u5143\u7d20 \u5728 Java \u4e2d\uff0c\u6570\u7ec4\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u56fa\u5b9a\u957f\u5ea6\u7684\u76f8\u540c\u6570\u636e\u7c7b\u578b\u5143\u7d20\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6570\u7ec4\u521b\u5efa\u540e\u5176\u5927\u5c0f\u4e0d\u53ef\u6539\u53d8\u3002\u7136\u800c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u65b0\u5143\u7d20\uff1a 1. \u6269\u5bb9\u5e76\u590d\u5236\u539f\u6709\u5143\u7d20 \u4e3a\u4e86\u5411\u73b0\u6709\u6570\u7ec4\u4e2d\u6dfb\u52a0\u66f4\u591a\u5143\u7d20\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u3001\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6709\u5143\u7d20\u590d\u5236\u5230\u5176\u4e2d\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] originalArray = {1, 2, 3}; int[] newArray = new int[originalArray.length + 1]; for (int i = 0; i &lt; originalArray.length; i++) { newArray[i] = originalArray[i]; } newArray[originalArray.length] = 4; \/\/ \u6dfb\u52a0\u65b0\u5143\u7d20 \/\/ \u66f4\u65b0\u539f\u6709\u6570\u7ec4\u7684\u5f15\u7528 originalArray = newArray; [&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-37912","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37912","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=37912"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37912\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}