{"id":38154,"date":"2024-11-26T08:25:03","date_gmt":"2024-11-26T00:25:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38154\/"},"modified":"2024-11-26T08:25:03","modified_gmt":"2024-11-26T00:25:03","slug":"java%e9%9b%86%e5%90%88%e8%bd%ac%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%bc%ba%e8%bd%ac","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38154\/","title":{"rendered":"java\u96c6\u5408\u8f6c\u6570\u7ec4\u600e\u4e48\u5f3a\u8f6c"},"content":{"rendered":"<blockquote><p>\n  java\u96c6\u5408\u53ef\u901a\u8fc7toarray()\u65b9\u6cd5\u76f4\u63a5\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u6216\u901a\u8fc7arrays.aslist()\u65b9\u6cd5\u53cd\u5411\u8f6c\u6362\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111409422116407.jpg\" class=\"aligncenter\" title=\"java\u96c6\u5408\u8f6c\u6570\u7ec4\u600e\u4e48\u5f3a\u8f6c\u63d2\u56fe\" alt=\"java\u96c6\u5408\u8f6c\u6570\u7ec4\u600e\u4e48\u5f3a\u8f6c\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\u96c6\u5408\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u4e00\u7cfb\u5217\u5143\u7d20\u3002\u6570\u7ec4\u4e5f\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u4e00\u7cfb\u5217\u5177\u6709\u76f8\u540c\u7c7b\u578b\u7684\u503c\u3002\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5c06\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6570\u7ec4\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5c06 Java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u4e24\u79cd\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 toArray() \u65b9\u6cd5<\/strong><\/p>\n<p>toArray() \u65b9\u6cd5\u662f\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u6307\u5b9a\u8fd4\u56de\u6570\u7ec4\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u96c6\u5408\u8f6c\u6362\u4e3a\u4e00\u4e2a\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>List&lt;String&gt; stringList = new ArrayList&lt;&gt;();\nstringList.add(\"Java\");\nstringList.add(\"Python\");\nstringList.add(\"C++\");\n\n\/\/ \u5c06\u5b57\u7b26\u4e32\u96c6\u5408\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\nString[] stringArray = stringList.toArray(new String[0]);\n\n\/\/ \u6253\u5370\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\nfor (String element : stringArray) {\n    System.out.println(element);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 Arrays.asList() \u65b9\u6cd5<\/strong><\/p>\n<p>Arrays.asList() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5217\u8868\u3002\u901a\u8fc7\u53cd\u5411\u4f7f\u7528\u8be5\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u96c6\u5408\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u4e00\u4e2a\u6574\u6570\u96c6\u5408\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff1a<\/p>\n<pre>List&lt;Integer&gt; integerList = new ArrayList&lt;&gt;();\nintegerList.add(1);\nintegerList.add(2);\nintegerList.add(3);\n\n\/\/ \u5c06\u6574\u6570\u96c6\u5408\u8f6c\u6362\u4e3a\u6574\u6570\u6570\u7ec4\nint[] integerArray = integerList.stream()\n        .mapToInt(Integer::intValue)\n        .toArray();\n\n\/\/ \u6253\u5370\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\nfor (int element : integerArray) {\n    System.out.println(element);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u4e24\u79cd\u5c06 Java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528 toArray() \u65b9\u6cd5\u548c\u4f7f\u7528 Arrays.asList() \u65b9\u6cd5\u3002\u7b2c\u4e00\u79cd\u65b9\u6cd5\u66f4\u52a0\u76f4\u63a5\uff0c\u800c\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u57fa\u672c\u7c7b\u578b\u6570\u7ec4\u7684\u7075\u6d3b\u65b9\u5f0f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u96c6\u5408\u8f6c\u6570\u7ec4\u600e\u4e48\u5f3a\u8f6c\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\u901a\u8fc7toarray()\u65b9\u6cd5\u76f4\u63a5\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u6216\u901a\u8fc7arrays.aslist()\u65b9\u6cd5\u53cd\u5411\u8f6c\u6362\u3002 \u5982\u4f55\u5c06 Java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u96c6\u5408\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u4e00\u7cfb\u5217\u5143\u7d20\u3002\u6570\u7ec4\u4e5f\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u4e00\u7cfb\u5217\u5177\u6709\u76f8\u540c\u7c7b\u578b\u7684\u503c\u3002\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5c06\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6570\u7ec4\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5c06 Java \u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u4e24\u79cd\u65b9\u6cd5\u3002 \u65b9\u6cd5 1\uff1a\u4f7f\u7528 toArray() \u65b9\u6cd5 toArray() \u65b9\u6cd5\u662f\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u6307\u5b9a\u8fd4\u56de\u6570\u7ec4\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u96c6\u5408\u8f6c\u6362\u4e3a\u4e00\u4e2a\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b List&lt;String&gt; stringList = new ArrayList&lt;&gt;(); stringList.add(&#8220;Java&#8221;); stringList.add(&#8220;Python&#8221;); stringList.add(&#8220;C++&#8221;); \/\/ \u5c06\u5b57\u7b26\u4e32\u96c6\u5408\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4 String[] stringArray = stringList.toArray(new String[0]); \/\/ \u6253\u5370\u6570\u7ec4\u4e2d\u7684\u5143\u7d20 for (String element : stringArray) { System.out.println(element); } \u767b\u5f55\u540e\u590d\u5236 \u65b9\u6cd5 2\uff1a\u4f7f\u7528 Arrays.asList() \u65b9\u6cd5 Arrays.asList() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5217\u8868\u3002\u901a\u8fc7\u53cd\u5411\u4f7f\u7528\u8be5\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u96c6\u5408\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u4e00\u4e2a\u6574\u6570\u96c6\u5408\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff1a List&lt;Integer&gt; integerList = new ArrayList&lt;&gt;(); integerList.add(1); integerList.add(2); [&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-38154","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38154","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=38154"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38154\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}