{"id":38138,"date":"2024-11-26T14:40:20","date_gmt":"2024-11-26T06:40:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38138\/"},"modified":"2024-11-26T14:40:20","modified_gmt":"2024-11-26T06:40:20","slug":"java%e4%b8%ad%e9%9b%86%e5%90%88%e6%80%8e%e4%b9%88%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38138\/","title":{"rendered":"java\u4e2d\u96c6\u5408\u600e\u4e48\u8f6c\u6362\u4e3a\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  java\u4e2d\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528toarray()\u65b9\u6cd5\uff0c\u5c06\u96c6\u5408\u5143\u7d20\u8f6c\u6362\u4e3a\u6307\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\u3002\u4f7f\u7528arrays.copyof()\u65b9\u6cd5\uff0c\u590d\u5236\u6307\u5b9a\u6570\u7ec4\u7684\u90e8\u5206\u6216\u5168\u90e8\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\u4f7f\u7528\u6d41\u5f0fapi\uff0c\u63d0\u4f9b\u66f4\u7b80\u6d01\u7684\u96c6\u5408\u8f6c\u6362\u65b9\u5f0f\u3002\u6570\u7ec4\u7c7b\u578b\u53ef\u901a\u8fc7\u6307\u5b9a\u6cdb\u578b\u6216\u4f7f\u7528object[]\u83b7\u53d6\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111409215029113.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u96c6\u5408\u600e\u4e48\u8f6c\u6362\u4e3a\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u4e2d\u96c6\u5408\u600e\u4e48\u8f6c\u6362\u4e3a\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u4e2d\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/p>\n<p>\u5c06Java\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528toArray()\u65b9\u6cd5<\/strong><\/p>\n<p>toArray()\u65b9\u6cd5\u662f\u96c6\u5408\u7c7b\u4e2d\u7684\u4e00\u4e2a\u5e38\u7528\u65b9\u6cd5\uff0c\u53ef\u5c06\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u6307\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>Collection&lt;String&gt; collection = new ArrayList&lt;&gt;();\nString[] array = collection.toArray(new String[collection.size()]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528Arrays.copyOf()\u65b9\u6cd5<\/strong><\/p>\n<p>Arrays.copyOf()\u65b9\u6cd5\u53ef\u5c06\u6307\u5b9a\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u6216\u5168\u90e8\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002<\/p>\n<pre>Object[] array = collection.toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528\u6d41\u5f0fAPI<\/strong><\/p>\n<p>Java 8\u5f15\u5165\u4e86\u6d41\u5f0fAPI\uff0c\u53ef\u63d0\u4f9b\u66f4\u7b80\u6d01\u7684\u96c6\u5408\u8f6c\u6362\u65b9\u5f0f\u3002<\/p>\n<pre>String[] array = collection.stream().toArray(String[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6570\u7ec4\u7c7b\u578b\u9009\u62e9<\/strong><\/p>\n<p>\u5728\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u6570\u7ec4\u7684\u7c7b\u578b\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u83b7\u53d6\u6570\u7ec4\u7c7b\u578b\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u6cdb\u578b\uff08\u5982\u679c\u96c6\u5408\u5177\u6709\u660e\u786e\u7684\u7c7b\u578b\u53c2\u6570\uff09\uff1a<\/li>\n<\/ul>\n<pre>Collection&lt;String&gt; collection = new ArrayList&lt;&gt;();\nString[] array = collection.toArray(new String[0]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u4f7f\u7528Object[]\uff08\u5982\u679c\u96c6\u5408\u5305\u542b\u5404\u79cd\u7c7b\u578b\u7684\u5143\u7d20\uff09\uff1a<\/li>\n<\/ul>\n<pre>Collection&lt;Object&gt; collection = new ArrayList&lt;&gt;();\nObject[] array = collection.toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u96c6\u5408\u4e3a\u7a7a\uff0ctoArray()\u65b9\u6cd5\u5c06\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6570\u7ec4\u3002<\/li>\n<li>toArray()\u65b9\u6cd5\u8fd4\u56de\u7684\u5bf9\u8c61\u6570\u7ec4\u7c7b\u578b\u4e3aObject[]\u3002\u5982\u679c\u9700\u8981\u7279\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u9700\u8981\u663e\u5f0f\u8f6c\u6362\u3002<\/li>\n<li>Arrays.copyOf()\u65b9\u6cd5\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u65b0\u6570\u7ec4\u7684\u5927\u5c0f\u3002\u5982\u679c\u6307\u5b9a\u7684\u503c\u5c0f\u4e8e\u96c6\u5408\u5927\u5c0f\uff0c\u5219\u591a\u4f59\u7684\u5143\u7d20\u5c06\u88ab\u622a\u65ad\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u96c6\u5408\u600e\u4e48\u8f6c\u6362\u4e3a\u6570\u7ec4\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\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528toarray()\u65b9\u6cd5\uff0c\u5c06\u96c6\u5408\u5143\u7d20\u8f6c\u6362\u4e3a\u6307\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\u3002\u4f7f\u7528arrays.copyof()\u65b9\u6cd5\uff0c\u590d\u5236\u6307\u5b9a\u6570\u7ec4\u7684\u90e8\u5206\u6216\u5168\u90e8\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\u4f7f\u7528\u6d41\u5f0fapi\uff0c\u63d0\u4f9b\u66f4\u7b80\u6d01\u7684\u96c6\u5408\u8f6c\u6362\u65b9\u5f0f\u3002\u6570\u7ec4\u7c7b\u578b\u53ef\u901a\u8fc7\u6307\u5b9a\u6cdb\u578b\u6216\u4f7f\u7528object[]\u83b7\u53d6\u3002 Java\u4e2d\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4 \u5c06Java\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a 1. \u4f7f\u7528toArray()\u65b9\u6cd5 toArray()\u65b9\u6cd5\u662f\u96c6\u5408\u7c7b\u4e2d\u7684\u4e00\u4e2a\u5e38\u7528\u65b9\u6cd5\uff0c\u53ef\u5c06\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u6307\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b Collection&lt;String&gt; collection = new ArrayList&lt;&gt;(); String[] array = collection.toArray(new String[collection.size()]); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528Arrays.copyOf()\u65b9\u6cd5 Arrays.copyOf()\u65b9\u6cd5\u53ef\u5c06\u6307\u5b9a\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u6216\u5168\u90e8\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002 Object[] array = collection.toArray(); \u767b\u5f55\u540e\u590d\u5236 3. \u4f7f\u7528\u6d41\u5f0fAPI Java 8\u5f15\u5165\u4e86\u6d41\u5f0fAPI\uff0c\u53ef\u63d0\u4f9b\u66f4\u7b80\u6d01\u7684\u96c6\u5408\u8f6c\u6362\u65b9\u5f0f\u3002 String[] array = collection.stream().toArray(String[]::new); \u767b\u5f55\u540e\u590d\u5236 \u6570\u7ec4\u7c7b\u578b\u9009\u62e9 \u5728\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u6570\u7ec4\u7684\u7c7b\u578b\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u83b7\u53d6\u6570\u7ec4\u7c7b\u578b\uff1a \u4f7f\u7528\u6cdb\u578b\uff08\u5982\u679c\u96c6\u5408\u5177\u6709\u660e\u786e\u7684\u7c7b\u578b\u53c2\u6570\uff09\uff1a Collection&lt;String&gt; collection = new ArrayList&lt;&gt;(); String[] array = collection.toArray(new String[0]); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528Object[]\uff08\u5982\u679c\u96c6\u5408\u5305\u542b\u5404\u79cd\u7c7b\u578b\u7684\u5143\u7d20\uff09\uff1a Collection&lt;Object&gt; collection = new ArrayList&lt;&gt;(); [&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-38138","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38138","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=38138"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38138\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}