{"id":66019,"date":"2025-05-03T15:10:05","date_gmt":"2025-05-03T07:10:05","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66019\/"},"modified":"2025-05-03T15:10:05","modified_gmt":"2025-05-03T07:10:05","slug":"java%e6%80%8e%e4%b9%88%e6%8a%8a%e9%9b%86%e5%90%88%e5%8f%98%e6%88%90%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66019\/","title":{"rendered":"java\u600e\u4e48\u628a\u96c6\u5408\u53d8\u6210\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u9488\u5bf9 java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u95ee\u9898\uff0c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1atoarray()\uff1a\u76f4\u63a5\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u6570\u7ec4\u7c7b\u578b\u53d6\u51b3\u4e8e\u96c6\u5408\u7c7b\u578b\u3002arrays.aslist()\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u518d\u4f7f\u7528 toarray() \u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u81ea\u5b9a\u4e49\u7c7b\u578b\u8f6c\u6362\uff1a\u4f7f\u7528\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u6216 stream api\uff0c\u5c06\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u6620\u5c04\u5230\u76ee\u6807\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111411242390709.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u628a\u96c6\u5408\u53d8\u6210\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u628a\u96c6\u5408\u53d8\u6210\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u8f6c\u6362<\/strong><\/p>\n<ul>\n<li> <strong>toArray() \u65b9\u6cd5\uff1a<\/strong>\u8be5\u65b9\u6cd5\u8fd4\u56de\u96c6\u5408\u4e2d\u5bf9\u8c61\u7684\u6570\u7ec4\u8868\u793a\u3002\u6570\u7ec4\u7684\u7c7b\u578b\u53d6\u51b3\u4e8e\u96c6\u5408\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>List&lt;String&gt; names = List.of(\"John\", \"Mary\", \"Bob\");\nString[] namesArray = names.toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Arrays.asList()<\/strong><\/p>\n<ul>\n<li> <strong>Arrays.asList() \u65b9\u6cd5\uff1a<\/strong>\u6b64\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u901a\u8fc7\u4f7f\u7528 toArray() \u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u53cd\u8f6c\u6b64\u8fc7\u7a0b\uff0c\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<pre>List&lt;String&gt; names = Arrays.asList(\"John\", \"Mary\", \"Bob\");\nString[] namesArray = names.toArray(new String[0]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u81ea\u5b9a\u4e49\u7c7b\u578b\u8f6c\u6362<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li> <strong>\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\uff1a<\/strong>\u5982\u679c\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u4e0d\u662f\u6570\u7ec4\u5143\u7d20\u671f\u671b\u7684\u7c7b\u578b\uff0c\u5219\u9700\u8981\u8fdb\u884c\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002<\/li>\n<\/ul>\n<pre>List&lt;Integer&gt; numbers = List.of(1, 2, 3);\nint[] numbersArray = new int[numbers.size()];\nfor (int i = 0; i &lt; numbers.size(); i++) {\n    numbersArray[i] = numbers.get(i);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u4f7f\u7528 Stream API\uff1a<\/strong>Stream API \u63d0\u4f9b\u4e86 mapToInt()\u3001mapToDouble() \u548c mapToLong() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u6620\u5c04\u5230\u76ee\u6807\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<pre>List&lt;Integer&gt; numbers = List.of(1, 2, 3);\nint[] numbersArray = numbers.stream().mapToInt(i -&gt; i).toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u9002\u5f53\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u96c6\u5408\u7684\u7c7b\u578b\u3001\u6570\u7ec4\u7684\u7c7b\u578b\u4ee5\u53ca\u7a0b\u5e8f\u7684\u6027\u80fd\u8981\u6c42\u3002\u5982\u679c\u60a8\u9700\u8981\u7acb\u5373\u83b7\u5f97\u6570\u7ec4\u8868\u793a\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 toArray() \u65b9\u6cd5\u3002\u5982\u679c\u60a8\u9700\u8981\u81ea\u5b9a\u4e49\u7c7b\u578b\u8f6c\u6362\uff0c\u5219\u9700\u8981\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u6216\u4f7f\u7528 Stream API\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u628a\u96c6\u5408\u53d8\u6210\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9488\u5bf9 java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u95ee\u9898\uff0c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1atoarray()\uff1a\u76f4\u63a5\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u6570\u7ec4\u7c7b\u578b\u53d6\u51b3\u4e8e\u96c6\u5408\u7c7b\u578b\u3002arrays.aslist()\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u518d\u4f7f\u7528 toarray() \u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u81ea\u5b9a\u4e49\u7c7b\u578b\u8f6c\u6362\uff1a\u4f7f\u7528\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u6216 stream api\uff0c\u5c06\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u6620\u5c04\u5230\u76ee\u6807\u6570\u7ec4\u3002 \u5982\u4f55\u5c06 Java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4 \u76f4\u63a5\u8f6c\u6362 toArray() \u65b9\u6cd5\uff1a\u8be5\u65b9\u6cd5\u8fd4\u56de\u96c6\u5408\u4e2d\u5bf9\u8c61\u7684\u6570\u7ec4\u8868\u793a\u3002\u6570\u7ec4\u7684\u7c7b\u578b\u53d6\u51b3\u4e8e\u96c6\u5408\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff1a List&lt;String&gt; names = List.of(&#8220;John&#8221;, &#8220;Mary&#8221;, &#8220;Bob&#8221;); String[] namesArray = names.toArray(); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 Arrays.asList() Arrays.asList() \u65b9\u6cd5\uff1a\u6b64\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u901a\u8fc7\u4f7f\u7528 toArray() \u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u53cd\u8f6c\u6b64\u8fc7\u7a0b\uff0c\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002 List&lt;String&gt; names = Arrays.asList(&#8220;John&#8221;, &#8220;Mary&#8221;, &#8220;Bob&#8221;); String[] namesArray = names.toArray(new String[0]); \u767b\u5f55\u540e\u590d\u5236 \u81ea\u5b9a\u4e49\u7c7b\u578b\u8f6c\u6362 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\uff1a\u5982\u679c\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u4e0d\u662f\u6570\u7ec4\u5143\u7d20\u671f\u671b\u7684\u7c7b\u578b\uff0c\u5219\u9700\u8981\u8fdb\u884c\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002 List&lt;Integer&gt; numbers = List.of(1, 2, 3); int[] numbersArray = new [&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-66019","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66019","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=66019"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66019\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}