{"id":39803,"date":"2024-11-26T16:45:26","date_gmt":"2024-11-26T08:45:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39803\/"},"modified":"2024-11-26T16:45:26","modified_gmt":"2024-11-26T08:45:26","slug":"java%e6%80%8e%e4%b9%88%e6%8a%8a%e9%9b%86%e5%90%88%e8%bd%ac%e5%8c%96%e6%88%90%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39803\/","title":{"rendered":"java\u600e\u4e48\u628a\u96c6\u5408\u8f6c\u5316\u6210\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  java \u96c6\u5408\u53ef\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u65b9\u6cd5\u5982\u4e0b\uff1a\u4f7f\u7528 toarray() \u83b7\u53d6 object[] \u6570\u7ec4\u3002\u4f7f\u7528\u6570\u7ec4\u7c7b\u578b\u53c2\u6570\u83b7\u53d6\u6307\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\u3002\u4f7f\u7528 stream.toarray() \u5c06 stream \u8f6c\u4e3a\u6570\u7ec4\u3002\u4f7f\u7528 arrays.aslist() \u5c06\u539f\u59cb\u7c7b\u578b\u6570\u7ec4\u8f6c\u4e3a\u5217\u8868\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111710335010527.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u628a\u96c6\u5408\u8f6c\u5316\u6210\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u628a\u96c6\u5408\u8f6c\u5316\u6210\u6570\u7ec4\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\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u6240\u7528\u96c6\u5408\u7684\u7c7b\u578b\u548c\u6240\u9700\u7684\u6570\u7ec4\u7c7b\u578b\u3002<\/p>\n<p><strong>1. \u4f7f\u7528 toArray() \u65b9\u6cd5<\/strong><\/p>\n<p>\u5bf9\u4e8e\u6240\u6709\u96c6\u5408\u7c7b\u578b\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528 toArray() \u65b9\u6cd5\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5305\u542b\u96c6\u5408\u5143\u7d20\u7684\u6570\u7ec4\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u6cdb\u578b Object[] \u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u6240\u6709\u96c6\u5408\u5143\u7d20\u7684\u5f15\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>List&lt;String&gt; names = List.of(\"John\", \"Mary\", \"Bob\");\nString[] namesArray = names.toArray(new String[0]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u6570\u7ec4\u7c7b\u578b\u53c2\u6570<\/strong><\/p>\n<p>\u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u578b\u7684\u96c6\u5408\uff08\u5982 ArrayList\uff09\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u7c7b\u578b\u53c2\u6570\u6765\u6307\u5b9a\u8981\u8f6c\u6362\u6210\u7684\u6570\u7ec4\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>List&lt;Integer&gt; numbers = List.of(1, 2, 3, 4, 5);\nInteger[] numbersArray = numbers.toArray(new Integer[0]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Stream.toArray() \u65b9\u6cd5<\/strong><\/p>\n<p>\u5bf9\u4e8e Stream\uff0c\u53ef\u4ee5\u4f7f\u7528 Stream.toArray() \u65b9\u6cd5\u5c06\u6d41\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6d41\u4e2d\u6240\u6709\u5143\u7d20\u7684\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>Stream&lt;String&gt; namesStream = Stream.of(\"John\", \"Mary\", \"Bob\");\nString[] namesArray = namesStream.toArray(String[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528 Arrays.asList() \u65b9\u6cd5<\/strong><\/p>\n<p>\u5bf9\u4e8e\u539f\u59cb\u7c7b\u578b\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.asList() \u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5305\u542b\u6570\u7ec4\u5143\u7d20\u7684\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>int[] numbersArray = {1, 2, 3, 4, 5};\nList&lt;Integer&gt; numbersList = Arrays.asList(numbersArray);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u628a\u96c6\u5408\u8f6c\u5316\u6210\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 \u96c6\u5408\u53ef\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u65b9\u6cd5\u5982\u4e0b\uff1a\u4f7f\u7528 toarray() \u83b7\u53d6 object[] \u6570\u7ec4\u3002\u4f7f\u7528\u6570\u7ec4\u7c7b\u578b\u53c2\u6570\u83b7\u53d6\u6307\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\u3002\u4f7f\u7528 stream.toarray() \u5c06 stream \u8f6c\u4e3a\u6570\u7ec4\u3002\u4f7f\u7528 arrays.aslist() \u5c06\u539f\u59cb\u7c7b\u578b\u6570\u7ec4\u8f6c\u4e3a\u5217\u8868\u3002 \u5982\u4f55\u5c06 Java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u6240\u7528\u96c6\u5408\u7684\u7c7b\u578b\u548c\u6240\u9700\u7684\u6570\u7ec4\u7c7b\u578b\u3002 1. \u4f7f\u7528 toArray() \u65b9\u6cd5 \u5bf9\u4e8e\u6240\u6709\u96c6\u5408\u7c7b\u578b\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528 toArray() \u65b9\u6cd5\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5305\u542b\u96c6\u5408\u5143\u7d20\u7684\u6570\u7ec4\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u6cdb\u578b Object[] \u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u6240\u6709\u96c6\u5408\u5143\u7d20\u7684\u5f15\u7528\u3002\u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b List&lt;String&gt; names = List.of(&#8220;John&#8221;, &#8220;Mary&#8221;, &#8220;Bob&#8221;); String[] namesArray = names.toArray(new String[0]); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528\u6570\u7ec4\u7c7b\u578b\u53c2\u6570 \u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u578b\u7684\u96c6\u5408\uff08\u5982 ArrayList\uff09\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u7c7b\u578b\u53c2\u6570\u6765\u6307\u5b9a\u8981\u8f6c\u6362\u6210\u7684\u6570\u7ec4\u7c7b\u578b\u3002\u4f8b\u5982\uff1a List&lt;Integer&gt; numbers = List.of(1, 2, 3, 4, 5); Integer[] numbersArray = [&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-39803","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39803","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=39803"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39803\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}