{"id":39717,"date":"2024-11-26T13:46:26","date_gmt":"2024-11-26T05:46:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39717\/"},"modified":"2024-11-26T13:46:26","modified_gmt":"2024-11-26T05:46:26","slug":"list%e6%80%8e%e4%b9%88%e8%bd%ac%e6%8d%a2%e6%88%90%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84-java","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39717\/","title":{"rendered":"list\u600e\u4e48\u8f6c\u6362\u6210\u5bf9\u8c61\u6570\u7ec4 java"},"content":{"rendered":"<blockquote><p>\n  \u5c06 list \u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u6709\u56db\u79cd\u65b9\u6cd5\uff1a1. \u4f7f\u7528 arrays.aslist() \u521b\u5efa\u6307\u5b9a\u7c7b\u578b\u7684\u65b0\u6570\u7ec4\u30022. \u4f7f\u7528 arrays.copyof() \u521b\u5efa\u4e0e list \u76f8\u540c\u5927\u5c0f\u7684\u65b0\u6570\u7ec4\u30023. \u4f7f\u7528 stream \u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u5927\u5c0f\u7531 list \u51b3\u5b9a\u30024. \u4f7f\u7528 object[] \u521b\u5efa\u5305\u542b list \u4e2d\u6240\u6709\u5143\u7d20\u7684\u5bf9\u8c61\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111622424174197.jpg\" class=\"aligncenter\" title=\"list\u600e\u4e48\u8f6c\u6362\u6210\u5bf9\u8c61\u6570\u7ec4 java\u63d2\u56fe\" alt=\"list\u600e\u4e48\u8f6c\u6362\u6210\u5bf9\u8c61\u6570\u7ec4 java\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 List \u8f6c\u6362\u4e3a\u7684\u5bf9\u8c61\u6570\u7ec4<\/strong><\/p>\n<p>Java \u4e2d\u5c06 List \u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5177\u4f53\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u60c5\u51b5\u3002<\/p>\n<p><strong>1. \u4f7f\u7528 Arrays.asList()<\/strong><\/p>\n<pre>List&lt;String&gt; list = new ArrayList&lt;&gt;();\nString[] array = list.toArray(new String[0]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65b9\u6cd5\u4f7f\u7528\u6307\u5b9a\u7c7b\u578b\u7684\u7a7a\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u5305\u542b List \u4e2d\u6240\u6709\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>2. \u4f7f\u7528 Arrays.copyOf()<\/strong><\/p>\n<pre>List&lt;Integer&gt; list = new ArrayList&lt;&gt;();\nInteger[] array = Arrays.copyOf(list.toArray(), list.size());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u5305\u542b List \u4e2d\u6240\u6709\u5143\u7d20\u7684\u65b0\u6570\u7ec4\uff0c\u6570\u7ec4\u5927\u5c0f\u4e0e List \u76f8\u540c\u3002<\/p>\n<p><strong>3. \u4f7f\u7528 Stream<\/strong><\/p>\n<pre>List&lt;Double&gt; list = new ArrayList&lt;&gt;();\nDouble[] array = list.stream().toArray(Double[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65b9\u6cd5\u4f7f\u7528 Stream API \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u5927\u5c0f\u7531 List \u7684\u5927\u5c0f\u51b3\u5b9a\u3002<\/p>\n<p><strong>4. \u4f7f\u7528 Object[]<\/strong><\/p>\n<pre>List&lt;Object&gt; list = new ArrayList&lt;&gt;();\nObject[] array = list.toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u5305\u542b List \u4e2d\u6240\u6709\u5143\u7d20\u7684\u5bf9\u8c61\u6570\u7ec4\u3002<\/p>\n<p><strong>\u63d0\u793a:<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd List \u4e2d\u7684\u5143\u7d20\u7c7b\u578b\u4e0e\u6570\u7ec4\u4e2d\u6307\u5b9a\u7684\u7c7b\u578b\u76f8\u5339\u914d\u3002<\/li>\n<li>\u5bf9\u4e8e\u539f\u59cb\u7c7b\u578b\uff08\u5982 int\u3001double\uff09\uff0c\u9700\u8981\u4f7f\u7528\u5176\u5305\u88c5\u5668\u7c7b\uff08\u5982 Integer\u3001Double\uff09\u3002<\/li>\n<li>\u5982\u679c List \u4e3a\u7a7a\uff0cArrays.asList() \u4f1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6570\u7ec4\uff0c\u800c Arrays.copyOf() \u5219\u4f1a\u5f15\u53d1 NullPointerException\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662flist\u600e\u4e48\u8f6c\u6362\u6210\u5bf9\u8c61\u6570\u7ec4 java\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>\u5c06 list \u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u6709\u56db\u79cd\u65b9\u6cd5\uff1a1. \u4f7f\u7528 arrays.aslist() \u521b\u5efa\u6307\u5b9a\u7c7b\u578b\u7684\u65b0\u6570\u7ec4\u30022. \u4f7f\u7528 arrays.copyof() \u521b\u5efa\u4e0e list \u76f8\u540c\u5927\u5c0f\u7684\u65b0\u6570\u7ec4\u30023. \u4f7f\u7528 stream \u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u5927\u5c0f\u7531 list \u51b3\u5b9a\u30024. \u4f7f\u7528 object[] \u521b\u5efa\u5305\u542b list \u4e2d\u6240\u6709\u5143\u7d20\u7684\u5bf9\u8c61\u6570\u7ec4\u3002 \u5982\u4f55\u5c06 List \u8f6c\u6362\u4e3a\u7684\u5bf9\u8c61\u6570\u7ec4 Java \u4e2d\u5c06 List \u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5177\u4f53\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u60c5\u51b5\u3002 1. \u4f7f\u7528 Arrays.asList() List&lt;String&gt; list = new ArrayList&lt;&gt;(); String[] array = list.toArray(new String[0]); \u767b\u5f55\u540e\u590d\u5236 \u6b64\u65b9\u6cd5\u4f7f\u7528\u6307\u5b9a\u7c7b\u578b\u7684\u7a7a\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u5305\u542b List \u4e2d\u6240\u6709\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 2. \u4f7f\u7528 Arrays.copyOf() List&lt;Integer&gt; list = new ArrayList&lt;&gt;(); Integer[] [&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-39717","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39717","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=39717"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39717\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}