{"id":34937,"date":"2024-11-26T17:33:40","date_gmt":"2024-11-26T09:33:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/34937\/"},"modified":"2024-11-26T17:33:40","modified_gmt":"2024-11-26T09:33:40","slug":"java%e4%b8%ad%e6%80%8e%e4%b9%88%e5%a2%9e%e5%8a%a0%e6%95%b0%e7%bb%84%e7%9a%84%e9%95%bf%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/34937\/","title":{"rendered":"java\u4e2d\u600e\u4e48\u589e\u52a0\u6570\u7ec4\u7684\u957f\u5ea6"},"content":{"rendered":"<blockquote><p>\n  \u65e0\u6cd5\u76f4\u63a5\u589e\u52a0 java \u6570\u7ec4\u957f\u5ea6\u3002\u66ff\u4ee3\u89e3\u51b3\u65b9\u6848\uff1a1. \u521b\u5efa\u6bd4\u73b0\u6709\u6570\u7ec4\u66f4\u5927\u4e14\u5305\u542b\u73b0\u6709\u5143\u7d20\u7684\u65b0\u6570\u7ec4\uff1b2. \u4f7f\u7528\u53ef\u53d8\u957f\u5ea6\u7684 arraylist\uff0c\u901a\u8fc7 ensurecapacity \u65b9\u6cd5\u52a8\u6001\u8c03\u6574\u957f\u5ea6\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110306242932681.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u600e\u4e48\u589e\u52a0\u6570\u7ec4\u7684\u957f\u5ea6\u63d2\u56fe\" alt=\"java\u4e2d\u600e\u4e48\u589e\u52a0\u6570\u7ec4\u7684\u957f\u5ea6\u63d2\u56fe\" \/><\/p>\n<h2>Java \u4e2d\u5982\u4f55\u589e\u52a0\u6570\u7ec4\u7684\u957f\u5ea6<\/h2>\n<p>Java \u4e2d\u7684\u6570\u7ec4\u662f\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u80fd\u6539\u53d8\u957f\u5ea6\u3002\u56e0\u6b64\uff0c\u65e0\u6cd5\u76f4\u63a5\u589e\u52a0\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/p>\n<p><strong>\u66ff\u4ee3\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u8981\u589e\u52a0\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u66ff\u4ee3\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u521b\u5efa\u6bd4\u73b0\u6709\u6570\u7ec4\u66f4\u5927\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u590d\u5236\u73b0\u6709\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5230\u65b0\u6570\u7ec4\u4e2d\u3002<\/p>\n<pre>int[] oldArray = {1, 2, 3};\nint[] newArray = new int[oldArray.length + 5];\n\n\/\/ \u590d\u5236\u5143\u7d20\nSystem.arraycopy(oldArray, 0, newArray, 0, oldArray.length);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 ArrayList\uff1a<\/strong><\/p>\n<p>ArrayList \u662f\u4e00\u79cd\u53ef\u53d8\u957f\u5ea6\u7684\u6570\u7ec4\uff0c\u5141\u8bb8\u52a8\u6001\u589e\u52a0\u548c\u5220\u9664\u5143\u7d20\u3002<\/p>\n<pre>ArrayList&lt;Integer&gt; arrayList = new ArrayList&lt;&gt;();\n\n\/\/ \u6dfb\u52a0\u5143\u7d20\narrayList.add(1);\narrayList.add(2);\narrayList.add(3);\n\n\/\/ \u589e\u52a0\u957f\u5ea6\narrayList.ensureCapacity(arrayList.size() + 5);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u600e\u4e48\u589e\u52a0\u6570\u7ec4\u7684\u957f\u5ea6\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>\u65e0\u6cd5\u76f4\u63a5\u589e\u52a0 java \u6570\u7ec4\u957f\u5ea6\u3002\u66ff\u4ee3\u89e3\u51b3\u65b9\u6848\uff1a1. \u521b\u5efa\u6bd4\u73b0\u6709\u6570\u7ec4\u66f4\u5927\u4e14\u5305\u542b\u73b0\u6709\u5143\u7d20\u7684\u65b0\u6570\u7ec4\uff1b2. \u4f7f\u7528\u53ef\u53d8\u957f\u5ea6\u7684 arraylist\uff0c\u901a\u8fc7 ensurecapacity \u65b9\u6cd5\u52a8\u6001\u8c03\u6574\u957f\u5ea6\u3002 Java \u4e2d\u5982\u4f55\u589e\u52a0\u6570\u7ec4\u7684\u957f\u5ea6 Java \u4e2d\u7684\u6570\u7ec4\u662f\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u80fd\u6539\u53d8\u957f\u5ea6\u3002\u56e0\u6b64\uff0c\u65e0\u6cd5\u76f4\u63a5\u589e\u52a0\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u66ff\u4ee3\u89e3\u51b3\u65b9\u6848\uff1a \u8981\u589e\u52a0\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u66ff\u4ee3\u89e3\u51b3\u65b9\u6848\uff1a 1. \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u521b\u5efa\u6bd4\u73b0\u6709\u6570\u7ec4\u66f4\u5927\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u590d\u5236\u73b0\u6709\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5230\u65b0\u6570\u7ec4\u4e2d\u3002 int[] oldArray = {1, 2, 3}; int[] newArray = new int[oldArray.length + 5]; \/\/ \u590d\u5236\u5143\u7d20 System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 ArrayList\uff1a ArrayList \u662f\u4e00\u79cd\u53ef\u53d8\u957f\u5ea6\u7684\u6570\u7ec4\uff0c\u5141\u8bb8\u52a8\u6001\u589e\u52a0\u548c\u5220\u9664\u5143\u7d20\u3002 ArrayList&lt;Integer&gt; arrayList = new ArrayList&lt;&gt;(); \/\/ \u6dfb\u52a0\u5143\u7d20 arrayList.add(1); arrayList.add(2); arrayList.add(3); \/\/ [&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-34937","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34937","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=34937"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34937\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=34937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=34937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=34937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}