{"id":37354,"date":"2024-11-26T16:16:59","date_gmt":"2024-11-26T08:16:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37354\/"},"modified":"2024-11-26T16:16:59","modified_gmt":"2024-11-26T08:16:59","slug":"java%e6%80%8e%e4%b9%88%e8%ae%be%e7%bd%ae%e5%8a%a8%e6%80%81%e6%95%b0%e7%bb%84%e5%a4%a7%e5%b0%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37354\/","title":{"rendered":"java\u600e\u4e48\u8bbe\u7f6e\u52a8\u6001\u6570\u7ec4\u5927\u5c0f"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u7684\u52a8\u6001\u6570\u7ec4\u5927\u5c0f\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u8bbe\u7f6e\uff1a\u6784\u9020\u51fd\u6570\uff1a\u6307\u5b9a\u521d\u59cb\u5927\u5c0f\u3002ensurecapacity() \u65b9\u6cd5\uff1a\u8c03\u6574\u5bb9\u91cf\u81f3\u6307\u5b9a\u5927\u5c0f\uff08\u5927\u4e8e\u5f53\u524d\u5bb9\u91cf\u624d\u8c03\u6574\uff09\u3002trimtosize() \u65b9\u6cd5\uff1a\u5c06\u5bb9\u91cf\u8c03\u6574\u4e3a\u5b9e\u9645\u5927\u5c0f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111304273374589.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u8bbe\u7f6e\u52a8\u6001\u6570\u7ec4\u5927\u5c0f\u63d2\u56fe\" alt=\"java\u600e\u4e48\u8bbe\u7f6e\u52a8\u6001\u6570\u7ec4\u5927\u5c0f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u8bbe\u7f6e Java \u4e2d\u52a8\u6001\u6570\u7ec4\u7684\u5927\u5c0f<\/strong><\/p>\n<p>\u52a8\u6001\u6570\u7ec4\uff08\u4e5f\u79f0\u4e3a ArrayList\uff09\u662f\u4e00\u79cd\u53ef\u6839\u636e\u9700\u8981\u8c03\u6574\u5927\u5c0f\u7684\u6570\u7ec4\u3002\u8981\u8bbe\u7f6e Java \u4e2d\u52a8\u6001\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528\u6784\u9020\u51fd\u6570<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u5728\u521b\u5efa ArrayList \u65f6\u4f7f\u7528\u6784\u9020\u51fd\u6570\u6307\u5b9a\u5176\u521d\u59cb\u5927\u5c0f\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;(10); \/\/ \u521d\u59cb\u5927\u5c0f\u4e3a 10<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 ensureCapacity() \u65b9\u6cd5<\/strong><\/p>\n<p>ensureCapacity() \u65b9\u6cd5\u4f1a\u5c06 ArrayList \u7684\u5bb9\u91cf\u8c03\u6574\u4e3a\u6307\u5b9a\u7684\u5927\u5c0f\u3002\u5982\u679c\u5f53\u524d\u5bb9\u91cf\u5c0f\u4e8e\u6307\u5b9a\u7684\u5927\u5c0f\uff0c\u5b83\u4f1a\u5c06\u5bb9\u91cf\u589e\u52a0\u5230\u6307\u5b9a\u5927\u5c0f\u3002\u5982\u679c\u5f53\u524d\u5bb9\u91cf\u5927\u4e8e\u6216\u7b49\u4e8e\u6307\u5b9a\u7684\u5927\u5c0f\uff0c\u5219\u4e0d\u4f1a\u53d1\u751f\u4efb\u4f55\u66f4\u6539\u3002<\/p>\n<pre>list.ensureCapacity(20); \/\/ \u5c06\u5bb9\u91cf\u589e\u52a0\u5230 20<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 trimToSize() \u65b9\u6cd5<\/strong><\/p>\n<p>trimToSize() \u65b9\u6cd5\u4f1a\u5c06 ArrayList \u7684\u5bb9\u91cf\u8c03\u6574\u4e3a\u5176\u5b9e\u9645\u5927\u5c0f\u3002\u8fd9\u610f\u5473\u7740\u5b83\u4f1a\u91ca\u653e\u4efb\u4f55\u672a\u4f7f\u7528\u7684\u7a7a\u95f4\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u3002<\/p>\n<pre>list.trimToSize(); \/\/ \u5c06\u5bb9\u91cf\u8c03\u6574\u4e3a\u5b9e\u9645\u5927\u5c0f<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u8bbe\u7f6e ArrayList \u7684\u5927\u5c0f\u4e0d\u4f1a\u5f71\u54cd\u5176\u5305\u542b\u7684\u5143\u7d20\u6570\u91cf\u3002<\/li>\n<li>\u5982\u679c\u5c06\u5bb9\u91cf\u8bbe\u7f6e\u4e3a\u5c0f\u4e8e\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff0c\u5219\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<li>\u8c03\u6574\u5bb9\u91cf\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u5f00\u9500\uff0c\u56e0\u6b64\u5728\u5fc5\u8981\u65f6\u518d\u4f7f\u7528\u5b83\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u8bbe\u7f6e\u52a8\u6001\u6570\u7ec4\u5927\u5c0f\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>java \u4e2d\u7684\u52a8\u6001\u6570\u7ec4\u5927\u5c0f\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u8bbe\u7f6e\uff1a\u6784\u9020\u51fd\u6570\uff1a\u6307\u5b9a\u521d\u59cb\u5927\u5c0f\u3002ensurecapacity() \u65b9\u6cd5\uff1a\u8c03\u6574\u5bb9\u91cf\u81f3\u6307\u5b9a\u5927\u5c0f\uff08\u5927\u4e8e\u5f53\u524d\u5bb9\u91cf\u624d\u8c03\u6574\uff09\u3002trimtosize() \u65b9\u6cd5\uff1a\u5c06\u5bb9\u91cf\u8c03\u6574\u4e3a\u5b9e\u9645\u5927\u5c0f\u3002 \u5982\u4f55\u8bbe\u7f6e Java \u4e2d\u52a8\u6001\u6570\u7ec4\u7684\u5927\u5c0f \u52a8\u6001\u6570\u7ec4\uff08\u4e5f\u79f0\u4e3a ArrayList\uff09\u662f\u4e00\u79cd\u53ef\u6839\u636e\u9700\u8981\u8c03\u6574\u5927\u5c0f\u7684\u6570\u7ec4\u3002\u8981\u8bbe\u7f6e Java \u4e2d\u52a8\u6001\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a 1. \u4f7f\u7528\u6784\u9020\u51fd\u6570 \u60a8\u53ef\u4ee5\u5728\u521b\u5efa ArrayList \u65f6\u4f7f\u7528\u6784\u9020\u51fd\u6570\u6307\u5b9a\u5176\u521d\u59cb\u5927\u5c0f\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;(10); \/\/ \u521d\u59cb\u5927\u5c0f\u4e3a 10 \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 ensureCapacity() \u65b9\u6cd5 ensureCapacity() \u65b9\u6cd5\u4f1a\u5c06 ArrayList \u7684\u5bb9\u91cf\u8c03\u6574\u4e3a\u6307\u5b9a\u7684\u5927\u5c0f\u3002\u5982\u679c\u5f53\u524d\u5bb9\u91cf\u5c0f\u4e8e\u6307\u5b9a\u7684\u5927\u5c0f\uff0c\u5b83\u4f1a\u5c06\u5bb9\u91cf\u589e\u52a0\u5230\u6307\u5b9a\u5927\u5c0f\u3002\u5982\u679c\u5f53\u524d\u5bb9\u91cf\u5927\u4e8e\u6216\u7b49\u4e8e\u6307\u5b9a\u7684\u5927\u5c0f\uff0c\u5219\u4e0d\u4f1a\u53d1\u751f\u4efb\u4f55\u66f4\u6539\u3002 list.ensureCapacity(20); \/\/ \u5c06\u5bb9\u91cf\u589e\u52a0\u5230 20 \u767b\u5f55\u540e\u590d\u5236 3. \u4f7f\u7528 trimToSize() \u65b9\u6cd5 trimToSize() \u65b9\u6cd5\u4f1a\u5c06 ArrayList \u7684\u5bb9\u91cf\u8c03\u6574\u4e3a\u5176\u5b9e\u9645\u5927\u5c0f\u3002\u8fd9\u610f\u5473\u7740\u5b83\u4f1a\u91ca\u653e\u4efb\u4f55\u672a\u4f7f\u7528\u7684\u7a7a\u95f4\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u3002 list.trimToSize(); \/\/ \u5c06\u5bb9\u91cf\u8c03\u6574\u4e3a\u5b9e\u9645\u5927\u5c0f \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\uff1a \u8bbe\u7f6e ArrayList \u7684\u5927\u5c0f\u4e0d\u4f1a\u5f71\u54cd\u5176\u5305\u542b\u7684\u5143\u7d20\u6570\u91cf\u3002 [&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-37354","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37354","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=37354"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37354\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}