{"id":66768,"date":"2025-05-03T10:31:16","date_gmt":"2025-05-03T02:31:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66768\/"},"modified":"2025-05-03T10:31:16","modified_gmt":"2025-05-03T02:31:16","slug":"%e4%b8%8d%e7%a1%ae%e5%ae%9a%e9%95%bf%e5%ba%a6%e7%9a%84%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e8%be%93%e5%85%a5java-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66768\/","title":{"rendered":"\u4e0d\u786e\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\u600e\u4e48\u8f93\u5165java"},"content":{"rendered":"<blockquote><p>\n  java \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8f93\u5165\u4e0d\u786e\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\uff1a\u521b\u5efa arraylist \u5e76\u4f7f\u7528 scanner \u8f93\u5165\u5143\u7d20\uff0c\u518d\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u5728 java 10 \u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u4f7f\u7528 varargs \u76f4\u63a5\u8f93\u5165\u6570\u7ec4\u3002\u4f7f\u7528 arrays.copyof() \u6839\u636e\u8f93\u5165\u5143\u7d20\u6570\u91cf\u521b\u5efa\u65b0\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111709004365396.jpg\" class=\"aligncenter\" title=\"\u4e0d\u786e\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\u600e\u4e48\u8f93\u5165java\u63d2\u56fe\" alt=\"\u4e0d\u786e\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\u600e\u4e48\u8f93\u5165java\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4e0d\u786e\u5b9a\u957f\u5ea6\u6570\u7ec4\u7684 Java \u8f93\u5165<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u4e00\u7cfb\u5217\u5143\u7d20\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u5e76\u4e14\u5728\u521b\u5efa\u6570\u7ec4\u65f6\u6307\u5b9a\u3002\u4f46\u662f\uff0c\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8f93\u5165\u4e0d\u786e\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 ArrayList<\/strong><\/p>\n<p>ArrayList \u662f Java \u4e2d\u4e00\u79cd\u53ef\u53d8\u957f\u5ea6\u7684\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u4f7f\u7528 ArrayList \u8f93\u5165\u4e0d\u786e\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a ArrayList\uff1a<\/p>\n<pre>ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u4f7f\u7528 Scanner \u8f93\u5165\u5143\u7d20\uff1a<\/p>\n<pre>Scanner scanner = new Scanner(System.in);\nwhile (scanner.hasNextInt()) {\nlist.add(scanner.nextInt());\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u8f6c\u6362 ArrayList \u4e3a\u6570\u7ec4\uff1a<\/p>\n<pre>int[] array = new int[list.size()];\nfor (int i = 0; i &lt; list.size(); i++) {\narray[i] = list.get(i);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>2. \u76f4\u63a5\u8f93\u5165\u5230\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java 10 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 varargs\uff08\u53ef\u53d8\u53c2\u6570\uff09\u76f4\u63a5\u8f93\u5165\u5230\u6570\u7ec4\u4e2d\uff1a<\/p>\n<pre>int[] array = {1, 2, 3, 4, 5};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Arrays.copyOf()<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u957f\u5ea6\u7b49\u4e8e\u8f93\u5165\u5143\u7d20\u7684\u6570\u91cf\uff1a<\/p>\n<pre>int[] array = Arrays.copyOf({1, 2, 3, 4, 5}, 5);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e0d\u786e\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\u600e\u4e48\u8f93\u5165java\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 \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8f93\u5165\u4e0d\u786e\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\uff1a\u521b\u5efa arraylist \u5e76\u4f7f\u7528 scanner \u8f93\u5165\u5143\u7d20\uff0c\u518d\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u5728 java 10 \u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u4f7f\u7528 varargs \u76f4\u63a5\u8f93\u5165\u6570\u7ec4\u3002\u4f7f\u7528 arrays.copyof() \u6839\u636e\u8f93\u5165\u5143\u7d20\u6570\u91cf\u521b\u5efa\u65b0\u6570\u7ec4\u3002 \u4e0d\u786e\u5b9a\u957f\u5ea6\u6570\u7ec4\u7684 Java \u8f93\u5165 \u5728 Java \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u4e00\u7cfb\u5217\u5143\u7d20\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u5e76\u4e14\u5728\u521b\u5efa\u6570\u7ec4\u65f6\u6307\u5b9a\u3002\u4f46\u662f\uff0c\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8f93\u5165\u4e0d\u786e\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 ArrayList ArrayList \u662f Java \u4e2d\u4e00\u79cd\u53ef\u53d8\u957f\u5ea6\u7684\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u4f7f\u7528 ArrayList \u8f93\u5165\u4e0d\u786e\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u521b\u5efa\u4e00\u4e2a ArrayList\uff1a ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;(); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 Scanner \u8f93\u5165\u5143\u7d20\uff1a Scanner scanner = new Scanner(System.in); while (scanner.hasNextInt()) { list.add(scanner.nextInt()); } \u767b\u5f55\u540e\u590d\u5236 \u8f6c\u6362 ArrayList \u4e3a\u6570\u7ec4\uff1a int[] [&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-66768","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66768","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=66768"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66768\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}