{"id":66524,"date":"2025-05-03T09:53:43","date_gmt":"2025-05-03T01:53:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66524\/"},"modified":"2025-05-03T09:53:43","modified_gmt":"2025-05-03T01:53:43","slug":"java-%e8%a6%81%e6%80%8e%e4%b9%88%e4%bf%ae%e6%94%b9%e6%95%b0%e7%bb%84%e9%95%bf%e5%ba%a6-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66524\/","title":{"rendered":"java \u8981\u600e\u4e48\u4fee\u6539\u6570\u7ec4\u957f\u5ea6"},"content":{"rendered":"<blockquote><p>\n  java\u6570\u7ec4\u957f\u5ea6\u4e0d\u53ef\u76f4\u63a5\u4fee\u6539\uff0c\u4f46\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u53d8\u76f8\u5b9e\u73b0\uff1a1. \u4f7f\u7528arraylist\u52a8\u6001\u6570\u7ec4\uff1b2. \u4f7f\u7528arrays.copyof()\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u4e0d\u540c\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\uff1b3. \u521b\u5efa\u4e00\u4e2a\u5177\u6709\u6240\u9700\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\u5e76\u590d\u5236\u539f\u59cb\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111605395951138.jpg\" class=\"aligncenter\" title=\"java \u8981\u600e\u4e48\u4fee\u6539\u6570\u7ec4\u957f\u5ea6\u63d2\u56fe\" alt=\"java \u8981\u600e\u4e48\u4fee\u6539\u6570\u7ec4\u957f\u5ea6\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4fee\u6539 Java \u6570\u7ec4\u7684\u957f\u5ea6\uff1f<\/strong><\/p>\n<p>Java \u6570\u7ec4\u7684\u957f\u5ea6\u4e00\u65e6\u521b\u5efa\uff0c\u5728\u7f16\u8bd1\u65f6\u5c31\u56fa\u5b9a\u4e86\u3002\u56e0\u6b64\uff0c\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u4f46\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u53d8\u76f8\u5730\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 ArrayList<\/strong><\/p>\n<p>ArrayList \u662f Java \u4e2d\u63d0\u4f9b\u7684\u52a8\u6001\u6570\u7ec4\u3002ArrayList \u7684\u957f\u5ea6\u53ef\u4ee5\u968f\u65f6\u4fee\u6539\uff0c\u5e76\u4e14\u81ea\u52a8\u5904\u7406\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a ArrayList\nArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();\n\n\/\/ \u6dfb\u52a0\u5143\u7d20\nlist.add(1);\nlist.add(2);\nlist.add(3);\n\n\/\/ \u68c0\u67e5\u5f53\u524d\u957f\u5ea6\nSystem.out.println(\"ArrayList length: \" + list.size());\n\n\/\/ \u589e\u52a0\u957f\u5ea6\nlist.ensureCapacity(10);\n\n\/\/ \u68c0\u67e5\u4fee\u6539\u540e\u7684\u957f\u5ea6\nSystem.out.println(\"ArrayList length after ensureCapacity: \" + list.size());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Arrays.copyOf()<\/strong><\/p>\n<p>Arrays.copyOf() \u65b9\u6cd5\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u539f\u59cb\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u6216\u5168\u90e8\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b64\u65b9\u6cd5\u521b\u5efa\u5177\u6709\u4e0d\u540c\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\u3002<\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a\u539f\u59cb\u6570\u7ec4\nint[] originalArray = {1, 2, 3};\n\n\/\/ \u4f7f\u7528 Arrays.copyOf() \u521b\u5efa\u4e00\u4e2a\u5177\u6709\u4e0d\u540c\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\nint[] newArray = Arrays.copyOf(originalArray, 5);\n\n\/\/ \u68c0\u67e5\u65b0\u6570\u7ec4\u7684\u957f\u5ea6\nSystem.out.println(\"New array length: \" + newArray.length);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u4fee\u6539\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u6240\u9700\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u590d\u5236\u539f\u59cb\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a\u539f\u59cb\u6570\u7ec4\nint[] originalArray = {1, 2, 3};\n\n\/\/ \u521b\u5efa\u4e00\u4e2a\u5177\u6709\u4e0d\u540c\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\nint[] newArray = new int[5];\n\n\/\/ \u590d\u5236\u5143\u7d20\nSystem.arraycopy(originalArray, 0, newArray, 0, originalArray.length);\n\n\/\/ \u68c0\u67e5\u65b0\u6570\u7ec4\u7684\u957f\u5ea6\nSystem.out.println(\"New array length: \" + newArray.length);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava \u8981\u600e\u4e48\u4fee\u6539\u6570\u7ec4\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>java\u6570\u7ec4\u957f\u5ea6\u4e0d\u53ef\u76f4\u63a5\u4fee\u6539\uff0c\u4f46\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u53d8\u76f8\u5b9e\u73b0\uff1a1. \u4f7f\u7528arraylist\u52a8\u6001\u6570\u7ec4\uff1b2. \u4f7f\u7528arrays.copyof()\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u4e0d\u540c\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\uff1b3. \u521b\u5efa\u4e00\u4e2a\u5177\u6709\u6240\u9700\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\u5e76\u590d\u5236\u539f\u59cb\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002 \u5982\u4f55\u4fee\u6539 Java \u6570\u7ec4\u7684\u957f\u5ea6\uff1f Java \u6570\u7ec4\u7684\u957f\u5ea6\u4e00\u65e6\u521b\u5efa\uff0c\u5728\u7f16\u8bd1\u65f6\u5c31\u56fa\u5b9a\u4e86\u3002\u56e0\u6b64\uff0c\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u4f46\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u53d8\u76f8\u5730\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\uff1a 1. \u4f7f\u7528 ArrayList ArrayList \u662f Java \u4e2d\u63d0\u4f9b\u7684\u52a8\u6001\u6570\u7ec4\u3002ArrayList \u7684\u957f\u5ea6\u53ef\u4ee5\u968f\u65f6\u4fee\u6539\uff0c\u5e76\u4e14\u81ea\u52a8\u5904\u7406\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \/\/ \u521b\u5efa\u4e00\u4e2a ArrayList ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;(); \/\/ \u6dfb\u52a0\u5143\u7d20 list.add(1); list.add(2); list.add(3); \/\/ \u68c0\u67e5\u5f53\u524d\u957f\u5ea6 System.out.println(&#8220;ArrayList length: &#8221; + list.size()); \/\/ \u589e\u52a0\u957f\u5ea6 list.ensureCapacity(10); \/\/ \u68c0\u67e5\u4fee\u6539\u540e\u7684\u957f\u5ea6 System.out.println(&#8220;ArrayList length after ensureCapacity: &#8221; + list.size()); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 Arrays.copyOf() Arrays.copyOf() [&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-66524","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66524","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=66524"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66524\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}