{"id":65985,"date":"2025-05-03T15:19:56","date_gmt":"2025-05-03T07:19:56","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65985\/"},"modified":"2025-05-03T15:19:56","modified_gmt":"2025-05-03T07:19:56","slug":"java%e5%b7%b2%e6%9c%89%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%8a%a0%e6%88%90%e5%91%98-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65985\/","title":{"rendered":"java\u5df2\u6709\u6570\u7ec4\u600e\u4e48\u52a0\u6210\u5458"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5411\u73b0\u6709\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20\uff1a1. \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5305\u542b\u539f\u6570\u7ec4\u548c\u65b0\u5143\u7d20\uff1b2. \u4f7f\u7528\u53ef\u53d8\u5927\u5c0f\u7684 arraylist\uff1b3. \u4f7f\u7528 apache commons lang \u5e93\u7684 arrayutils.add() \u65b9\u6cd5\uff1b4. \u4f7f\u7528 guava \u5e93\u7684 lists.newarraylist() \u65b9\u6cd5\uff1b5. \uff08\u4e0d\u63a8\u8350\uff09\u901a\u8fc7 java \u53cd\u5c04\u7ed5\u8fc7\u6570\u7ec4\u4e0d\u53ef\u53d8\u7684\u9650\u5236\u5e76\u76f4\u63a5\u4fee\u6539\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111410071810270.jpg\" class=\"aligncenter\" title=\"java\u5df2\u6709\u6570\u7ec4\u600e\u4e48\u52a0\u6210\u5458\u63d2\u56fe\" alt=\"java\u5df2\u6709\u6570\u7ec4\u600e\u4e48\u52a0\u6210\u5458\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5411 Java \u4e2d\u7684\u73b0\u6709\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u6570\u7ec4\u662f\u4e00\u79cd\u56fa\u5b9a\u5927\u5c0f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4e00\u65e6\u521b\u5efa\uff0c\u5c31\u4e0d\u80fd\u4fee\u6539\u5176\u5927\u5c0f\u3002\u4f46\u662f\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5411\u73b0\u6709\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4<\/strong><\/p>\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u73b0\u6709\u6570\u7ec4\u6240\u6709\u5143\u7d20\u4ee5\u53ca\u65b0\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] oldArray = {1, 2, 3};\nint newArray[] = new int[oldArray.length + 1];\n\nfor (int i = 0; i &lt; oldArray.length; i++) {\n    newArray[i] = oldArray[i];\n}\nnewArray[newArray.length - 1] = 4;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 ArrayList<\/strong><\/p>\n<p>ArrayList \u662f\u4e00\u4e2a\u53ef\u53d8\u5927\u5c0f\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u3002<\/p>\n<pre>List&lt;Integer&gt; list = Arrays.asList(1, 2, 3);\nlist.add(4);\nint[] newArray = list.stream().mapToInt(Integer::intValue).toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Apache Commons Lang \u5e93<\/strong><\/p>\n<p>Apache Commons Lang \u5e93\u63d0\u4f9b\u4e86 ArrayUtils.add() \u65b9\u6cd5\uff0c\u53ef\u5c06\u4e00\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u6570\u7ec4\u7684\u672b\u5c3e\u3002<\/p>\n<pre>int[] oldArray = {1, 2, 3};\nint[] newArray = ArrayUtils.add(oldArray, 4);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528 Guava \u5e93<\/strong><\/p>\n<p>Guava \u5e93\u63d0\u4f9b\u4e86 Lists.newArrayList() \u65b9\u6cd5\uff0c\u53ef\u590d\u5236\u4e00\u4e2a\u6570\u7ec4\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u53ef\u53d8\u5927\u5c0f\u7684 ArrayList\u3002<\/p>\n<pre>int[] oldArray = {1, 2, 3};\nList&lt;Integer&gt; list = Lists.newArrayList(oldArray);\nlist.add(4);\nint[] newArray = Lists.toArray(list);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u4f7f\u7528\u53cd\u5c04\u8fdb\u884c\u4fee\u6539<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 Java \u53cd\u5c04\uff0c\u53ef\u4ee5\u7ed5\u8fc7\u6570\u7ec4\u4e0d\u53ef\u53d8\u7684\u9650\u5236\u5e76\u76f4\u63a5\u4fee\u6539\u6570\u7ec4\u3002\u4f46\u662f\uff0c\u6b64\u65b9\u6cd5\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u4f1a\u7834\u574f\u6570\u7ec4\u7684\u7c7b\u578b\u5b89\u5168\u3002<\/p>\n<pre>int[] oldArray = {1, 2, 3};\n\n\/\/ \u4f7f\u7528\u53cd\u5c04\u83b7\u53d6\u6570\u7ec4\u7684 Field \u5bf9\u8c61\nField field = oldArray.getClass().getDeclaredField(\"length\");\n\n\/\/ \u8bbe\u7f6e\u6570\u7ec4\u7684\u957f\u5ea6\nfield.setAccessible(true);\nfield.setInt(oldArray, oldArray.length + 1);\n\n\/\/ \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u6700\u540e\u4e00\u4e2a\u7d22\u5f15\noldArray[oldArray.length - 1] = 4;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u5df2\u6709\u6570\u7ec4\u600e\u4e48\u52a0\u6210\u5458\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>\u5728 java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5411\u73b0\u6709\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20\uff1a1. \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5305\u542b\u539f\u6570\u7ec4\u548c\u65b0\u5143\u7d20\uff1b2. \u4f7f\u7528\u53ef\u53d8\u5927\u5c0f\u7684 arraylist\uff1b3. \u4f7f\u7528 apache commons lang \u5e93\u7684 arrayutils.add() \u65b9\u6cd5\uff1b4. \u4f7f\u7528 guava \u5e93\u7684 lists.newarraylist() \u65b9\u6cd5\uff1b5. \uff08\u4e0d\u63a8\u8350\uff09\u901a\u8fc7 java \u53cd\u5c04\u7ed5\u8fc7\u6570\u7ec4\u4e0d\u53ef\u53d8\u7684\u9650\u5236\u5e76\u76f4\u63a5\u4fee\u6539\u6570\u7ec4\u3002 \u5982\u4f55\u5411 Java \u4e2d\u7684\u73b0\u6709\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20 \u5728 Java \u4e2d\uff0c\u6570\u7ec4\u662f\u4e00\u79cd\u56fa\u5b9a\u5927\u5c0f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4e00\u65e6\u521b\u5efa\uff0c\u5c31\u4e0d\u80fd\u4fee\u6539\u5176\u5927\u5c0f\u3002\u4f46\u662f\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5411\u73b0\u6709\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20\uff1a 1. \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4 \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u73b0\u6709\u6570\u7ec4\u6240\u6709\u5143\u7d20\u4ee5\u53ca\u65b0\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] oldArray = {1, 2, 3}; int newArray[] = new int[oldArray.length + 1]; for (int i = 0; i &lt; oldArray.length; i++) { newArray[i] [&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-65985","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65985","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=65985"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65985\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}