{"id":64175,"date":"2025-05-03T10:51:12","date_gmt":"2025-05-03T02:51:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64175\/"},"modified":"2025-05-03T10:51:12","modified_gmt":"2025-05-03T02:51:12","slug":"java-%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%8a%a0%e9%95%bf%e5%ba%a6-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64175\/","title":{"rendered":"java \u5b57\u7b26\u4e32\u6570\u7ec4\u600e\u4e48\u52a0\u957f\u5ea6"},"content":{"rendered":"<blockquote><p>\n  \u6269\u5c55 java \u5b57\u7b26\u4e32\u6570\u7ec4\u957f\u5ea6\u7684 3 \u79cd\u65b9\u6cd5\uff1a\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u590d\u5236\u5143\u7d20\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\u4f7f\u7528 arrays.copyof() \u65b9\u6cd5\u521b\u5efa\u65b0\u6570\u7ec4\u3002\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a arraylist\uff0c\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\uff0c\u518d\u8f6c\u6362\u4e3a\u65b0\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/02\/2024110217122622094.jpg\" class=\"aligncenter\" title=\"java \u5b57\u7b26\u4e32\u6570\u7ec4\u600e\u4e48\u52a0\u957f\u5ea6\u63d2\u56fe\" alt=\"java \u5b57\u7b26\u4e32\u6570\u7ec4\u600e\u4e48\u52a0\u957f\u5ea6\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u6269\u5c55 Java \u7684\u957f\u5ea6<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u5b57\u7b26\u4e32\u6570\u7ec4\u662f\u4e00\u7ec4\u6709\u5e8f\u7684\u5b57\u7b26\u4e32\u5143\u7d20\u3002\u521b\u5efa\u6570\u7ec4\u540e\uff0c\u5143\u7d20\u6570\u91cf\u662f\u56fa\u5b9a\u7684\uff0c\u65e0\u6cd5\u76f4\u63a5\u6269\u5c55\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u6269\u5c55\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u957f\u5ea6\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u5927\u4e8e\u539f\u6570\u7ec4\u7684\u65b0\u6570\u7ec4\u3002<\/li>\n<li>\u904d\u5386\u539f\u6570\u7ec4\u5e76\u590d\u5236\u5143\u7d20\u5230\u65b0\u6570\u7ec4\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>String[] originalArray = {\"A\", \"B\", \"C\"};\nint newLength = originalArray.length + 2;\n\nString[] newArray = new String[newLength];\n\nfor (int i = 0; i &lt; originalArray.length; i++) {\n    newArray[i] = originalArray[i];\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Arrays.copyOf()<\/strong><\/p>\n<ul>\n<li>Java \u63d0\u4f9b\u4e86 Arrays.copyOf() \u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u539f\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u6216\u5168\u90e8\u5143\u7d20\u3002<\/li>\n<li>\u6307\u5b9a\u6240\u9700\u7684\u957f\u5ea6\u4f5c\u4e3a\u53c2\u6570\uff0c\u8be5\u65b9\u6cd5\u4f1a\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u8be5\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>String[] originalArray = {\"A\", \"B\", \"C\"};\nint newLength = originalArray.length + 2;\n\nString[] newArray = Arrays.copyOf(originalArray, newLength);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 ArrayList<\/strong><\/p>\n<ul>\n<li>ArrayList \u662f\u4e00\u4e2a\u53ef\u53d8\u957f\u5ea6\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u3002<\/li>\n<li>\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a ArrayList\uff0c\u7136\u540e\u5728\u9700\u8981\u65f6\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002<\/li>\n<li>\u6700\u540e\uff0c\u53ef\u4ee5\u5c06 ArrayList \u8f6c\u6362\u4e3a\u65b0\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>String[] originalArray = {\"A\", \"B\", \"C\"};\n\nList&lt;String&gt; arrayList = new ArrayList&lt;&gt;(Arrays.asList(originalArray));\narrayList.add(\"D\");\narrayList.add(\"E\");\n\nString[] newArray = arrayList.toArray(new String[0]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava \u600e\u4e48\u52a0\u957f\u5ea6\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>\u6269\u5c55 java \u5b57\u7b26\u4e32\u6570\u7ec4\u957f\u5ea6\u7684 3 \u79cd\u65b9\u6cd5\uff1a\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u590d\u5236\u5143\u7d20\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\u4f7f\u7528 arrays.copyof() \u65b9\u6cd5\u521b\u5efa\u65b0\u6570\u7ec4\u3002\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a arraylist\uff0c\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\uff0c\u518d\u8f6c\u6362\u4e3a\u65b0\u6570\u7ec4\u3002 \u5982\u4f55\u6269\u5c55 Java \u7684\u957f\u5ea6 \u5728 Java \u4e2d\uff0c\u5b57\u7b26\u4e32\u6570\u7ec4\u662f\u4e00\u7ec4\u6709\u5e8f\u7684\u5b57\u7b26\u4e32\u5143\u7d20\u3002\u521b\u5efa\u6570\u7ec4\u540e\uff0c\u5143\u7d20\u6570\u91cf\u662f\u56fa\u5b9a\u7684\uff0c\u65e0\u6cd5\u76f4\u63a5\u6269\u5c55\u3002 \u4f46\u662f\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u6269\u5c55\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u957f\u5ea6\uff1a 1. \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u5927\u4e8e\u539f\u6570\u7ec4\u7684\u65b0\u6570\u7ec4\u3002 \u904d\u5386\u539f\u6570\u7ec4\u5e76\u590d\u5236\u5143\u7d20\u5230\u65b0\u6570\u7ec4\u4e2d\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a String[] originalArray = {&#8220;A&#8221;, &#8220;B&#8221;, &#8220;C&#8221;}; int newLength = originalArray.length + 2; String[] newArray = new String[newLength]; for (int i = 0; i &lt; originalArray.length; i++) { newArray[i] = originalArray[i]; } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 [&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-64175","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64175","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=64175"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64175\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}