{"id":65350,"date":"2025-05-03T14:56:28","date_gmt":"2025-05-03T06:56:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65350\/"},"modified":"2025-05-03T14:56:28","modified_gmt":"2025-05-03T06:56:28","slug":"java-%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e8%bd%aclist-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65350\/","title":{"rendered":"java \u6570\u7ec4\u600e\u4e48\u8f6clist"},"content":{"rendered":"<blockquote><p>\n  java \u6570\u7ec4\u8f6c list \u7684\u65b9\u6cd5\u6709\u56db\u79cd\uff1aarrays.aslist() \u8fd4\u56de\u4e0d\u53ef\u53d8 list\u3001list.of() \u8fd4\u56de\u53ef\u53d8 list\uff08java 9 \u53ca\u4ee5\u4e0a\uff09\u3001\u5faa\u73af\u521b\u5efa list\u3001guava \u7684 lists.newarraylist()\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111221342229679.jpg\" class=\"aligncenter\" title=\"java \u6570\u7ec4\u600e\u4e48\u8f6clist\u63d2\u56fe\" alt=\"java \u6570\u7ec4\u600e\u4e48\u8f6clist\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\u8f6c List<\/strong><\/p>\n<p>Java \u6570\u7ec4\u8f6c List \u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5177\u4f53\u9009\u62e9\u6839\u636e\u5b9e\u9645\u9700\u6c42\u800c\u5b9a\u3002<\/p>\n<p><strong>1. \u4f7f\u7528 Arrays.asList()<\/strong><\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nList&lt;Integer&gt; list = Arrays.asList(arr);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u7b80\u5355\u6613\u7528\uff0c\u4e0d\u9700\u8981\u521b\u5efa\u65b0\u7684 List \u5bf9\u8c61\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u8fd4\u56de\u7684 List \u65e0\u6cd5\u4fee\u6539\uff0c\u56e0\u4e3a\u5e95\u5c42\u6570\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\u3002<\/p>\n<p><strong>2. \u4f7f\u7528 List.of()<\/strong><\/p>\n<p>Java 9 \u53ca\u4ee5\u4e0a\u7248\u672c\u53ef\u4ee5\u4f7f\u7528 List.of() \u65b9\u6cd5\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nList&lt;Integer&gt; list = List.of(arr);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u4e0e Arrays.asList() \u7c7b\u4f3c\uff0c\u4f46\u8fd4\u56de\u7684 List \u53ef\u53d8\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u4ec5\u9002\u7528\u4e8e Java 9 \u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/p>\n<p><strong>3. \u4f7f\u7528\u5faa\u73af<\/strong><\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nList&lt;Integer&gt; list = new ArrayList&lt;&gt;();\nfor (int num : arr) {\n    list.add(num);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u624b\u52a8\u521b\u5efa List\uff0c\u53ef\u4ee5\u5b9a\u5236 List \u7684\u7c7b\u578b\u548c\u7279\u6027\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u4ee3\u7801\u5197\u957f\uff0c\u9700\u8981\u624b\u52a8\u904d\u5386\u6570\u7ec4\u3002<\/p>\n<p><strong>4. \u4f7f\u7528 Guava<\/strong><\/p>\n<p>Guava \u5e93\u63d0\u4f9b\u4e86 Lists.newArrayList() \u65b9\u6cd5\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nList&lt;Integer&gt; list = Lists.newArrayList(arr);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u7b80\u5355\u6613\u7528\uff0c\u8fd4\u56de\u7684 List \u53ef\u53d8\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u9700\u8981\u6dfb\u52a0 Guava \u4f9d\u8d56\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava \u6570\u7ec4\u600e\u4e48\u8f6clist\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>java \u6570\u7ec4\u8f6c list \u7684\u65b9\u6cd5\u6709\u56db\u79cd\uff1aarrays.aslist() \u8fd4\u56de\u4e0d\u53ef\u53d8 list\u3001list.of() \u8fd4\u56de\u53ef\u53d8 list\uff08java 9 \u53ca\u4ee5\u4e0a\uff09\u3001\u5faa\u73af\u521b\u5efa list\u3001guava \u7684 lists.newarraylist()\u3002 Java \u6570\u7ec4\u8f6c List Java \u6570\u7ec4\u8f6c List \u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5177\u4f53\u9009\u62e9\u6839\u636e\u5b9e\u9645\u9700\u6c42\u800c\u5b9a\u3002 1. \u4f7f\u7528 Arrays.asList() int[] arr = {1, 2, 3, 4, 5}; List&lt;Integer&gt; list = Arrays.asList(arr); \u767b\u5f55\u540e\u590d\u5236 \u4f18\u70b9\uff1a\u7b80\u5355\u6613\u7528\uff0c\u4e0d\u9700\u8981\u521b\u5efa\u65b0\u7684 List \u5bf9\u8c61\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u7f3a\u70b9\uff1a\u8fd4\u56de\u7684 List \u65e0\u6cd5\u4fee\u6539\uff0c\u56e0\u4e3a\u5e95\u5c42\u6570\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\u3002 2. \u4f7f\u7528 List.of() Java 9 \u53ca\u4ee5\u4e0a\u7248\u672c\u53ef\u4ee5\u4f7f\u7528 List.of() \u65b9\u6cd5\uff1a int[] arr = {1, [&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-65350","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65350","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=65350"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65350\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}