{"id":37255,"date":"2024-11-26T14:39:52","date_gmt":"2024-11-26T06:39:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37255\/"},"modified":"2024-11-26T14:39:52","modified_gmt":"2024-11-26T06:39:52","slug":"java%e5%8a%a8%e6%80%81%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37255\/","title":{"rendered":"java\u52a8\u6001\u6570\u7ec4\u600e\u4e48\u6784\u9020\u51fd\u6570"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d arraylist \u7684\u6784\u9020\u51fd\u6570\u5305\u62ec\uff1a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff1a\u521b\u5efa\u521d\u59cb\u5bb9\u91cf\u4e3a 10 \u7684\u7a7a\u6570\u7ec4\u5217\u8868\u3002\u6307\u5b9a\u5bb9\u91cf\u7684\u6784\u9020\u51fd\u6570\uff1a\u521b\u5efa\u6307\u5b9a\u521d\u59cb\u5bb9\u91cf\u7684\u7a7a\u6570\u7ec4\u5217\u8868\u3002\u4ece\u96c6\u5408\u6784\u9020\u7684\u6784\u9020\u51fd\u6570\uff1a\u521b\u5efa\u5305\u542b\u6307\u5b9a\u96c6\u5408\u4e2d\u6240\u6709\u5143\u7d20\u7684\u6570\u7ec4\u5217\u8868\u3002\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u7684\u7279\u5b9a\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6784\u9020\u51fd\u6570\uff0c\u4ee5\u4f18\u5316\u6027\u80fd\u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111303063297751.jpg\" class=\"aligncenter\" title=\"java\u52a8\u6001\u6570\u7ec4\u600e\u4e48\u6784\u9020\u51fd\u6570\u63d2\u56fe\" alt=\"java\u52a8\u6001\u6570\u7ec4\u600e\u4e48\u6784\u9020\u51fd\u6570\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u52a8\u6001\u6570\u7ec4\u7684\u6784\u9020\u51fd\u6570<\/strong><\/p>\n<p>Java \u4e2d\u7684\u52a8\u6001\u6570\u7ec4\u7531 ArrayList \u7c7b\u8868\u793a\u3002\u5b83\u662f\u4e00\u4e2a\u53ef\u53d8\u957f\u5ea6\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u52a8\u6001\u5730\u589e\u52a0\u6216\u51cf\u5c11\u5176\u5927\u5c0f\u3002<\/p>\n<p>\u8981\u521b\u5efa ArrayList \u5b9e\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6784\u9020\u51fd\u6570\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff1aArrayList()<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u5bb9\u91cf\u4e3a 10 \u7684\u7a7a\u6570\u7ec4\u5217\u8868\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6307\u5b9a\u5bb9\u91cf\u7684\u6784\u9020\u51fd\u6570\uff1aArrayList(int initialCapacity)<\/strong><\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u5bb9\u91cf\u4e3a initialCapacity \u7684\u7a7a\u6570\u7ec4\u5217\u8868\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4ece\u96c6\u5408\u6784\u9020\u7684\u6784\u9020\u51fd\u6570\uff1aArrayList(Collection extends E&gt; c)<\/strong><\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6307\u5b9a\u96c6\u5408 c \u4e2d\u6240\u6709\u5143\u7d20\u7684\u6570\u7ec4\u5217\u8868\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u6784\u9020\u51fd\u6570<\/strong><\/p>\n<p>\u9009\u62e9\u6700\u5408\u9002\u7684\u6784\u9020\u51fd\u6570\u53d6\u51b3\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u7279\u5b9a\u9700\u6c42\uff1a<\/p>\n<ul>\n<li>\u5982\u679c\u9884\u8ba1\u6570\u7ec4\u5217\u8868\u7684\u5927\u5c0f\u4f1a\u76f8\u5bf9\u8f83\u5c0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002<\/li>\n<li>\u5982\u679c\u5df2\u77e5\u6570\u7ec4\u5217\u8868\u7684\u521d\u59cb\u5927\u5c0f\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u6307\u5b9a\u5bb9\u91cf\u7684\u6784\u9020\u51fd\u6570\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u5206\u914d\u3002<\/li>\n<li>\u5982\u679c\u8981\u5c06\u73b0\u6709\u7684\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ece\u96c6\u5408\u6784\u9020\u7684\u6784\u9020\u51fd\u6570\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u663e\u793a\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u6784\u9020\u51fd\u6570\uff1a<\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u5bb9\u91cf\u4e3a 10 \u7684\u7a7a\u6570\u7ec4\u5217\u8868\nArrayList&lt;String&gt; list1 = new ArrayList&lt;&gt;();\n\n\/\/ \u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u5bb9\u91cf\u4e3a 20 \u7684\u7a7a\u6570\u7ec4\u5217\u8868\nArrayList&lt;Integer&gt; list2 = new ArrayList&lt;&gt;(20);\n\n\/\/ \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6307\u5b9a\u96c6\u5408\u4e2d\u6240\u6709\u5143\u7d20\u7684\u6570\u7ec4\u5217\u8868\nSet&lt;String&gt; set = new HashSet&lt;&gt;();\nset.add(\"A\");\nset.add(\"B\");\nset.add(\"C\");\nArrayList&lt;String&gt; list3 = new ArrayList&lt;&gt;(set);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6839\u636e\u5177\u4f53\u60c5\u51b5\uff0c\u9009\u62e9\u5408\u9002\u7684\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u4f18\u5316 ArrayList \u7684\u6027\u80fd\u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u52a8\u6001\u6570\u7ec4\u600e\u4e48\u6784\u9020\u51fd\u6570\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 arraylist \u7684\u6784\u9020\u51fd\u6570\u5305\u62ec\uff1a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff1a\u521b\u5efa\u521d\u59cb\u5bb9\u91cf\u4e3a 10 \u7684\u7a7a\u6570\u7ec4\u5217\u8868\u3002\u6307\u5b9a\u5bb9\u91cf\u7684\u6784\u9020\u51fd\u6570\uff1a\u521b\u5efa\u6307\u5b9a\u521d\u59cb\u5bb9\u91cf\u7684\u7a7a\u6570\u7ec4\u5217\u8868\u3002\u4ece\u96c6\u5408\u6784\u9020\u7684\u6784\u9020\u51fd\u6570\uff1a\u521b\u5efa\u5305\u542b\u6307\u5b9a\u96c6\u5408\u4e2d\u6240\u6709\u5143\u7d20\u7684\u6570\u7ec4\u5217\u8868\u3002\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u7684\u7279\u5b9a\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6784\u9020\u51fd\u6570\uff0c\u4ee5\u4f18\u5316\u6027\u80fd\u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002 Java \u52a8\u6001\u6570\u7ec4\u7684\u6784\u9020\u51fd\u6570 Java \u4e2d\u7684\u52a8\u6001\u6570\u7ec4\u7531 ArrayList \u7c7b\u8868\u793a\u3002\u5b83\u662f\u4e00\u4e2a\u53ef\u53d8\u957f\u5ea6\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u52a8\u6001\u5730\u589e\u52a0\u6216\u51cf\u5c11\u5176\u5927\u5c0f\u3002 \u8981\u521b\u5efa ArrayList \u5b9e\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6784\u9020\u51fd\u6570\uff1a \u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff1aArrayList() \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u5bb9\u91cf\u4e3a 10 \u7684\u7a7a\u6570\u7ec4\u5217\u8868\u3002 \u6307\u5b9a\u5bb9\u91cf\u7684\u6784\u9020\u51fd\u6570\uff1aArrayList(int initialCapacity) \u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u5bb9\u91cf\u4e3a initialCapacity \u7684\u7a7a\u6570\u7ec4\u5217\u8868\u3002 \u4ece\u96c6\u5408\u6784\u9020\u7684\u6784\u9020\u51fd\u6570\uff1aArrayList(Collection extends E&gt; c) \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6307\u5b9a\u96c6\u5408 c \u4e2d\u6240\u6709\u5143\u7d20\u7684\u6570\u7ec4\u5217\u8868\u3002 \u9009\u62e9\u5408\u9002\u7684\u6784\u9020\u51fd\u6570 \u9009\u62e9\u6700\u5408\u9002\u7684\u6784\u9020\u51fd\u6570\u53d6\u51b3\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u7279\u5b9a\u9700\u6c42\uff1a \u5982\u679c\u9884\u8ba1\u6570\u7ec4\u5217\u8868\u7684\u5927\u5c0f\u4f1a\u76f8\u5bf9\u8f83\u5c0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002 \u5982\u679c\u5df2\u77e5\u6570\u7ec4\u5217\u8868\u7684\u521d\u59cb\u5927\u5c0f\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u6307\u5b9a\u5bb9\u91cf\u7684\u6784\u9020\u51fd\u6570\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u5206\u914d\u3002 \u5982\u679c\u8981\u5c06\u73b0\u6709\u7684\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ece\u96c6\u5408\u6784\u9020\u7684\u6784\u9020\u51fd\u6570\u3002 \u793a\u4f8b \u4ee5\u4e0b\u793a\u4f8b\u663e\u793a\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u6784\u9020\u51fd\u6570\uff1a \/\/ \u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u5bb9\u91cf\u4e3a 10 \u7684\u7a7a\u6570\u7ec4\u5217\u8868 ArrayList&lt;String&gt; list1 = new ArrayList&lt;&gt;(); \/\/ \u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u5bb9\u91cf\u4e3a 20 \u7684\u7a7a\u6570\u7ec4\u5217\u8868 ArrayList&lt;Integer&gt; list2 = [&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-37255","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37255","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=37255"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37255\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}