{"id":36886,"date":"2024-11-26T16:41:59","date_gmt":"2024-11-26T08:41:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36886\/"},"modified":"2024-11-26T16:41:59","modified_gmt":"2024-11-26T08:41:59","slug":"java%e6%80%8e%e4%b9%88%e9%9b%86%e5%90%88%e8%bd%ac%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36886\/","title":{"rendered":"java\u600e\u4e48\u96c6\u5408\u8f6c\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5c06 java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 toarray() \u65b9\u6cd5\u8f6c\u6362\u4e3a object \u6570\u7ec4\uff0c\u9700\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u4f7f\u7528 stream api \u8f6c\u6362\u4e3a intstream\uff0c\u518d\u8f6c\u6362\u4e3a int[] \u6570\u7ec4\uff0c\u540c\u6837\u9700\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111220543963899.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u96c6\u5408\u8f6c\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u96c6\u5408\u8f6c\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/p>\n<p>\u8981\u5c06 Java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 toArray() \u65b9\u6cd5<\/strong><\/p>\n<p>toArray() \u65b9\u6cd5\u662f Collection \u63a5\u53e3\u4e2d\u7684\u4e00\u4e2a\u901a\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a Object \u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u5b83\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a\u6574\u6570\u96c6\u5408\nList&lt;Integer&gt; numbers = List.of(1, 2, 3, 4, 5);\n\n\/\/ \u5c06\u96c6\u5408\u8f6c\u6362\u4e3a Object \u6570\u7ec4\nObject[] numbersArray = numbers.toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4f7f\u7528 toArray() \u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u5b83\u8fd4\u56de\u4e00\u4e2a Object \u6570\u7ec4\uff0c\u5373\u4f7f\u96c6\u5408\u4e2d\u5305\u542b\u7684\u662f\u7279\u5b9a\u7c7b\u578b\u7684\u5143\u7d20\u3002\u8981\u5c06 Object \u6570\u7ec4\u8f6c\u6362\u4e3a\u7279\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff1a<\/p>\n<pre>Integer[] numbersArray = (Integer[]) numbers.toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 Stream API<\/strong><\/p>\n<p>Stream API \u4e5f\u63d0\u4f9b\u4e86\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u5b83\uff1a<\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a\u6574\u6570\u96c6\u5408\nList&lt;Integer&gt; numbers = List.of(1, 2, 3, 4, 5);\n\n\/\/ \u5c06\u96c6\u5408\u8f6c\u6362\u4e3a IntStream\uff0c\u5176\u4e2d\u5305\u542b\u96c6\u5408\u4e2d\u7684\u5143\u7d20\nIntStream numbersStream = numbers.stream().mapToInt(Integer::intValue);\n\n\/\/ \u5c06 IntStream \u8f6c\u6362\u4e3a int[] \u6570\u7ec4\nint[] numbersArray = numbersStream.toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0e toArray() \u65b9\u6cd5\u7c7b\u4f3c\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a Object[] \u6570\u7ec4\uff0c\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff1a<\/p>\n<pre>Integer[] numbersArray = Arrays.stream(numbersStream.toArray()).boxed().toArray(Integer[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u65f6\uff0ctoArray() \u65b9\u6cd5\u5bf9\u4e8e\u8f83\u5c0f\u7684\u96c6\u5408\u6765\u8bf4\u6548\u7387\u66f4\u9ad8\uff0c\u800c Stream API \u5bf9\u4e8e\u8f83\u5927\u7684\u96c6\u5408\u6765\u8bf4\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u96c6\u5408\u8f6c\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>\u5c06 java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 toarray() \u65b9\u6cd5\u8f6c\u6362\u4e3a object \u6570\u7ec4\uff0c\u9700\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u4f7f\u7528 stream api \u8f6c\u6362\u4e3a intstream\uff0c\u518d\u8f6c\u6362\u4e3a int[] \u6570\u7ec4\uff0c\u540c\u6837\u9700\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002 \u5982\u4f55\u5c06 Java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4 \u8981\u5c06 Java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a \u65b9\u6cd5 1\uff1a\u4f7f\u7528 toArray() \u65b9\u6cd5 toArray() \u65b9\u6cd5\u662f Collection \u63a5\u53e3\u4e2d\u7684\u4e00\u4e2a\u901a\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a Object \u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u5b83\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \/\/ \u521b\u5efa\u4e00\u4e2a\u6574\u6570\u96c6\u5408 List&lt;Integer&gt; numbers = List.of(1, 2, 3, 4, 5); \/\/ \u5c06\u96c6\u5408\u8f6c\u6362\u4e3a Object \u6570\u7ec4 Object[] numbersArray = numbers.toArray(); \u767b\u5f55\u540e\u590d\u5236 \u5728\u4f7f\u7528 toArray() \u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u5b83\u8fd4\u56de\u4e00\u4e2a Object \u6570\u7ec4\uff0c\u5373\u4f7f\u96c6\u5408\u4e2d\u5305\u542b\u7684\u662f\u7279\u5b9a\u7c7b\u578b\u7684\u5143\u7d20\u3002\u8981\u5c06 Object [&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-36886","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36886","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=36886"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36886\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}