{"id":38203,"date":"2024-11-26T08:21:02","date_gmt":"2024-11-26T00:21:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38203\/"},"modified":"2024-11-26T08:21:02","modified_gmt":"2024-11-26T00:21:02","slug":"java%e4%b8%ad%e7%9a%84%e9%9b%86%e5%90%88%e6%80%8e%e4%b9%88%e8%bd%ac%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38203\/","title":{"rendered":"java\u4e2d\u7684\u96c6\u5408\u600e\u4e48\u8f6c\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728java\u4e2d\u8f6c\u6362\u96c6\u5408\u4e3a\u6570\u7ec4\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528toarray()\u65b9\u6cd5\uff1a\u8fd4\u56de\u5305\u542b\u96c6\u5408\u5143\u7d20\u7684\u6570\u7ec4\u3002\u624b\u52a8\u8f6c\u6362\uff1a\u521b\u5efa\u4e0e\u96c6\u5408\u5927\u5c0f\u76f8\u540c\u7684\u6570\u7ec4\uff0c\u5e76\u9010\u4e2a\u590d\u5236\u5143\u7d20\u3002\u4f7f\u7528 guava\u5e93\u7684collections2.toarray()\u65b9\u6cd5\uff1a\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6307\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111410371789403.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u7684\u96c6\u5408\u600e\u4e48\u8f6c\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u4e2d\u7684\u96c6\u5408\u600e\u4e48\u8f6c\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u96c6\u5408\u8f6c\u6570\u7ec4<\/strong><\/p>\n<p>Java\u4e2d\uff0c\u96c6\u5408\u662f\u7528\u4e8e\u5b58\u50a8\u6570\u636e\u7684\u5bf9\u8c61\u96c6\u5408\u3002\u8981\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. toArray()\u65b9\u6cd5<\/strong><\/p>\n<p>toArray()\u65b9\u6cd5\u662f\u96c6\u5408\u7c7b\u4e2d\u6700\u5e38\u7528\u7684\u8f6c\u6362\u65b9\u6cd5\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u96c6\u5408\u4e2d\u6240\u6709\u5143\u7d20\u7684\u6570\u7ec4\u3002\u6570\u7ec4\u7684\u7c7b\u578b\u53d6\u51b3\u4e8e\u96c6\u5408\u7684\u7c7b\u578b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u96c6\u5408\nSet&lt;String&gt; set = new HashSet&lt;&gt;();\nset.add(\"Java\");\nset.add(\"Python\");\nset.add(\"C++\");\n\n\/\/ \u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\nString[] array = set.toArray(new String[0]);\n\n\/\/ \u6253\u5370\u6570\u7ec4\nfor (String s : array) {\n    System.out.println(s);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u624b\u52a8\u8f6c\u6362<\/strong><\/p>\n<p>\u4e5f\u53ef\u4ee5\u624b\u52a8\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4e3a\u6b64\uff0c\u9700\u8981\u521b\u5efa\u4e0e\u96c6\u5408\u5927\u5c0f\u76f8\u540c\u7684\u6570\u7ec4\uff0c\u7136\u540e\u9010\u4e2a\u590d\u5236\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u5230\u6570\u7ec4\u4e2d\uff1a<\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a\u6574\u578b\u96c6\u5408\nList&lt;Integer&gt; list = new ArrayList&lt;&gt;();\nlist.add(1);\nlist.add(2);\nlist.add(3);\n\n\/\/ \u521b\u5efa\u4e00\u4e2a\u4e0e\u96c6\u5408\u5927\u5c0f\u76f8\u540c\u7684\u6574\u578b\u6570\u7ec4\nint[] array = new int[list.size()];\n\n\/\/ \u5c06\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u590d\u5236\u5230\u6570\u7ec4\u4e2d\nfor (int i = 0; i &lt; list.size(); i++) {\n    array[i] = list.get(i);\n}\n\n\/\/ \u6253\u5370\u6570\u7ec4\nfor (int n : array) {\n    System.out.println(n);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528Guava\u5e93<\/strong><\/p>\n<p>Guava\u5e93\u63d0\u4f9b\u4e86Collections2.toArray()\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6307\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\uff1a<\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u96c6\u5408\nSet&lt;String&gt; set = new HashSet&lt;&gt;();\nset.add(\"Java\");\nset.add(\"Python\");\nset.add(\"C++\");\n\n\/\/ \u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\nString[] array = Collections2.toArray(set, String.class);\n\n\/\/ \u6253\u5370\u6570\u7ec4\nfor (String s : array) {\n    System.out.println(s);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u8f6c\u6362\u65b9\u6cd5\u53d6\u51b3\u4e8e\u96c6\u5408\u7684\u7c7b\u578b\u548c\u6240\u9700\u7684\u8f6c\u6362\u901f\u5ea6\u3002toArray()\u65b9\u6cd5\u901a\u5e38\u662f\u6700\u65b9\u4fbf\u548c\u6700\u5feb\u7684\uff0c\u4f46\u5982\u679c\u9700\u8981\u6307\u5b9a\u6570\u7ec4\u7684\u7c7b\u578b\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u624b\u52a8\u8f6c\u6362\u6216Guava\u5e93\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u7684\u96c6\u5408\u600e\u4e48\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>\u5728java\u4e2d\u8f6c\u6362\u96c6\u5408\u4e3a\u6570\u7ec4\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528toarray()\u65b9\u6cd5\uff1a\u8fd4\u56de\u5305\u542b\u96c6\u5408\u5143\u7d20\u7684\u6570\u7ec4\u3002\u624b\u52a8\u8f6c\u6362\uff1a\u521b\u5efa\u4e0e\u96c6\u5408\u5927\u5c0f\u76f8\u540c\u7684\u6570\u7ec4\uff0c\u5e76\u9010\u4e2a\u590d\u5236\u5143\u7d20\u3002\u4f7f\u7528 guava\u5e93\u7684collections2.toarray()\u65b9\u6cd5\uff1a\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6307\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\u3002 Java\u96c6\u5408\u8f6c\u6570\u7ec4 Java\u4e2d\uff0c\u96c6\u5408\u662f\u7528\u4e8e\u5b58\u50a8\u6570\u636e\u7684\u5bf9\u8c61\u96c6\u5408\u3002\u8981\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a 1. toArray()\u65b9\u6cd5 toArray()\u65b9\u6cd5\u662f\u96c6\u5408\u7c7b\u4e2d\u6700\u5e38\u7528\u7684\u8f6c\u6362\u65b9\u6cd5\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u96c6\u5408\u4e2d\u6240\u6709\u5143\u7d20\u7684\u6570\u7ec4\u3002\u6570\u7ec4\u7684\u7c7b\u578b\u53d6\u51b3\u4e8e\u96c6\u5408\u7684\u7c7b\u578b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \/\/ \u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u96c6\u5408 Set&lt;String&gt; set = new HashSet&lt;&gt;(); set.add(&#8220;Java&#8221;); set.add(&#8220;Python&#8221;); set.add(&#8220;C++&#8221;); \/\/ \u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4 String[] array = set.toArray(new String[0]); \/\/ \u6253\u5370\u6570\u7ec4 for (String s : array) { System.out.println(s); } \u767b\u5f55\u540e\u590d\u5236 2. \u624b\u52a8\u8f6c\u6362 \u4e5f\u53ef\u4ee5\u624b\u52a8\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4e3a\u6b64\uff0c\u9700\u8981\u521b\u5efa\u4e0e\u96c6\u5408\u5927\u5c0f\u76f8\u540c\u7684\u6570\u7ec4\uff0c\u7136\u540e\u9010\u4e2a\u590d\u5236\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u5230\u6570\u7ec4\u4e2d\uff1a \/\/ \u521b\u5efa\u4e00\u4e2a\u6574\u578b\u96c6\u5408 List&lt;Integer&gt; list = new ArrayList&lt;&gt;(); list.add(1); list.add(2); list.add(3); \/\/ \u521b\u5efa\u4e00\u4e2a\u4e0e\u96c6\u5408\u5927\u5c0f\u76f8\u540c\u7684\u6574\u578b\u6570\u7ec4 int[] array = 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-38203","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38203","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=38203"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38203\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}