{"id":40246,"date":"2024-11-26T08:17:51","date_gmt":"2024-11-26T00:17:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40246\/"},"modified":"2024-11-26T08:17:51","modified_gmt":"2024-11-26T00:17:51","slug":"java%e6%80%8e%e4%b9%88%e5%8f%96list%e9%87%8c%e7%9a%84%e4%b8%80%e4%b8%aa%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/40246\/","title":{"rendered":"java\u600e\u4e48\u53d6list\u91cc\u7684\u4e00\u4e2a\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u4ece java list \u4e2d\u83b7\u53d6\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a\u76f4\u63a5\u8f6c\u6362\u3001\u5229\u7528 collection.toarray()\u3001\u5229\u7528 java 8+ stream api\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/18\/2024111814151991077.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u53d6list\u91cc\u7684\u4e00\u4e2a\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u53d6list\u91cc\u7684\u4e00\u4e2a\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4ece Java List \u4e2d\u83b7\u53d6\u6570\u7ec4<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u65b9\u6cd5\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u5c06 List \u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a<\/p>\n<pre>int[] array = list.toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>\u5229\u7528 Collection.toArray() \u65b9\u6cd5\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528 Collection.toArray() \u65b9\u6cd5\u5c06 List \u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a int[] \u6570\u7ec4\uff1a<\/p>\n<pre>Integer[] objectArray = list.toArray(new Integer[0]);\nint[] array = new int[objectArray.length];\nfor (int i = 0; i &lt; objectArray.length; i++) {\n    array[i] = objectArray[i];\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>\u5229\u7528 Java 8+ Stream API\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528 Java 8+ Stream API \u5c06 List \u4e2d\u7684\u5143\u7d20\u6620\u5c04\u5230 int[] \u6570\u7ec4\uff1a<\/p>\n<pre>int[] array = list.stream().mapToInt(Integer::intValue).toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>List&lt;Integer&gt; list = Arrays.asList(1, 2, 3, 4, 5);\nint[] array = list.toArray(); \/\/ \u76f4\u63a5\u65b9\u6cd5\nint[] array = list.stream().mapToInt(Integer::intValue).toArray(); \/\/ Java 8+ Stream API<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u679c List \u4e2d\u5305\u542b\u975e int \u5143\u7d20\uff0c\u5219\u4ee5\u4e0a\u65b9\u6cd5\u5c06\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u83b7\u53d6\u539f\u59cb\u7c7b\u578b\u6570\u7ec4\uff08\u4f8b\u5982 byte[]\u3001char[]\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5e94\u7684 .toArray() \u65b9\u6cd5\uff0c\u4f8b\u5982 list.toArray(new byte[0])\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u53d6list\u91cc\u7684\u4e00\u4e2a\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>\u4ece java list \u4e2d\u83b7\u53d6\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a\u76f4\u63a5\u8f6c\u6362\u3001\u5229\u7528 collection.toarray()\u3001\u5229\u7528 java 8+ stream api\u3002 \u5982\u4f55\u4ece Java List \u4e2d\u83b7\u53d6\u6570\u7ec4 \u76f4\u63a5\u65b9\u6cd5\uff1a \u5c06 List \u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a int[] array = list.toArray(); \u767b\u5f55\u540e\u590d\u5236 \u5229\u7528 Collection.toArray() \u65b9\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4f7f\u7528 Collection.toArray() \u65b9\u6cd5\u5c06 List \u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a int[] \u6570\u7ec4\uff1a Integer[] objectArray = list.toArray(new Integer[0]); int[] array = new int[objectArray.length]; for (int i = 0; i &lt; objectArray.length; i++) { array[i] = [&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-40246","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40246","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=40246"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40246\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}