{"id":34960,"date":"2024-11-26T16:32:19","date_gmt":"2024-11-26T08:32:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/34960\/"},"modified":"2024-11-26T16:32:19","modified_gmt":"2024-11-26T08:32:19","slug":"java%e9%9b%86%e5%90%88%e6%80%8e%e4%b9%88%e8%bd%ac%e6%8d%a2%e6%88%90%e6%95%b0%e7%bb%84%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/34960\/","title":{"rendered":"java\u96c6\u5408\u600e\u4e48\u8f6c\u6362\u6210\u6570\u7ec4\u4e2d"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u56db\u79cd\u65b9\u6cd5\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a\u4f7f\u7528 toarray() \u65b9\u6cd5\u4f7f\u7528 list.toarray(t[] array) \u65b9\u6cd5\u4f7f\u7528 stream.toarray() \u65b9\u6cd5\u624b\u52a8\u521b\u5efa\u6570\u7ec4\u5e76\u9010\u4e2a\u6dfb\u52a0\u5143\u7d20\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110306571993445.jpg\" class=\"aligncenter\" title=\"java\u96c6\u5408\u600e\u4e48\u8f6c\u6362\u6210\u6570\u7ec4\u4e2d\u63d2\u56fe\" alt=\"java\u96c6\u5408\u600e\u4e48\u8f6c\u6362\u6210\u6570\u7ec4\u4e2d\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 toArray() \u65b9\u6cd5<\/strong><\/p>\n<p>\u8fd9\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u53ea\u9700\u8c03\u7528\u96c6\u5408\u7c7b\u7684 toArray() \u65b9\u6cd5\u5373\u53ef\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u96c6\u5408\u4e2d\u6240\u6709\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u65b0\u6570\u7ec4\u7684\u7c7b\u578b\u662f Object[]\uff0c\u4f46\u4e5f\u53ef\u4ee5\u6307\u5b9a\u6240\u9700\u6570\u7ec4\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>List&lt;Integer&gt; list = new ArrayList&lt;&gt;();\nlist.add(1);\nlist.add(2);\nlist.add(3);\n\nInteger[] arr = list.toArray(new Integer[list.size()]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 List.toArray(T[] array) \u65b9\u6cd5<\/strong><\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4e0e toArray() \u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u5b83\u5141\u8bb8\u6307\u5b9a\u8981\u521b\u5efa\u7684\u65b0\u6570\u7ec4\u7684\u7c7b\u578b\u3002\u5982\u679c\u8981\u521b\u5efa\u7684\u6570\u7ec4\u7684\u7c7b\u578b\u4e0e\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u7c7b\u578b\u4e0d\u5339\u914d\uff0c\u5219\u4f1a\u5f15\u53d1 ClassCastException\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>List&lt;Integer&gt; list = new ArrayList&lt;&gt;();\nlist.add(1);\nlist.add(2);\nlist.add(3);\n\nString[] arr = list.toArray(new String[list.size()]); \/\/ \u4f1a\u5f15\u53d1 ClassCastException<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Stream.toArray() \u65b9\u6cd5<\/strong><\/p>\n<p>Java 8 \u5f15\u5165\u4e86 Stream API\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6d41\u5f0f\u5904\u7406\u96c6\u5408\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528 Stream.toArray() \u65b9\u6cd5\u5c06\u6d41\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>List&lt;Integer&gt; list = new ArrayList&lt;&gt;();\nlist.add(1);\nlist.add(2);\nlist.add(3);\n\nInteger[] arr = list.stream().toArray(Integer[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u624b\u52a8\u521b\u5efa\u6570\u7ec4\u5e76\u9010\u4e2a\u6dfb\u52a0\u5143\u7d20<\/strong><\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u6bd4\u8f83\u7e41\u7410\uff0c\u9700\u8981\u624b\u52a8\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\uff0c\u7136\u540e\u9010\u4e2a\u5c06\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>List&lt;Integer&gt; list = new ArrayList&lt;&gt;();\nlist.add(1);\nlist.add(2);\nlist.add(3);\n\nint[] arr = new int[list.size()];\nfor (int i = 0; i &lt; list.size(); i++) {\n    arr[i] = list.get(i);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u96c6\u5408\u600e\u4e48\u8f6c\u6362\u6210\u6570\u7ec4\u4e2d\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>\u5728 java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u56db\u79cd\u65b9\u6cd5\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a\u4f7f\u7528 toarray() \u65b9\u6cd5\u4f7f\u7528 list.toarray(t[] array) \u65b9\u6cd5\u4f7f\u7528 stream.toarray() \u65b9\u6cd5\u624b\u52a8\u521b\u5efa\u6570\u7ec4\u5e76\u9010\u4e2a\u6dfb\u52a0\u5143\u7d20 \u5982\u4f55\u5c06 Java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 toArray() \u65b9\u6cd5 \u8fd9\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u53ea\u9700\u8c03\u7528\u96c6\u5408\u7c7b\u7684 toArray() \u65b9\u6cd5\u5373\u53ef\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u96c6\u5408\u4e2d\u6240\u6709\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u65b0\u6570\u7ec4\u7684\u7c7b\u578b\u662f Object[]\uff0c\u4f46\u4e5f\u53ef\u4ee5\u6307\u5b9a\u6240\u9700\u6570\u7ec4\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b List&lt;Integer&gt; list = new ArrayList&lt;&gt;(); list.add(1); list.add(2); list.add(3); Integer[] arr = list.toArray(new Integer[list.size()]); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 List.toArray(T[] array) \u65b9\u6cd5 \u8fd9\u4e2a\u65b9\u6cd5\u4e0e toArray() \u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u5b83\u5141\u8bb8\u6307\u5b9a\u8981\u521b\u5efa\u7684\u65b0\u6570\u7ec4\u7684\u7c7b\u578b\u3002\u5982\u679c\u8981\u521b\u5efa\u7684\u6570\u7ec4\u7684\u7c7b\u578b\u4e0e\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u7c7b\u578b\u4e0d\u5339\u914d\uff0c\u5219\u4f1a\u5f15\u53d1 ClassCastException\u3002\u4f8b\u5982\uff1a List&lt;Integer&gt; list = new ArrayList&lt;&gt;(); list.add(1); list.add(2); [&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-34960","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34960","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=34960"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34960\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=34960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=34960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=34960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}