{"id":37933,"date":"2024-11-26T10:58:31","date_gmt":"2024-11-26T02:58:31","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37933\/"},"modified":"2024-11-26T10:58:31","modified_gmt":"2024-11-26T02:58:31","slug":"java%e6%80%8e%e4%b9%88%e4%bd%bf%e6%95%b0%e7%bb%84%e9%95%bf%e5%ba%a6%e5%a2%9e%e5%8a%a0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37933\/","title":{"rendered":"java\u600e\u4e48\u4f7f\u6570\u7ec4\u957f\u5ea6\u589e\u52a0"},"content":{"rendered":"<blockquote><p>\n  java \u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u52a8\u6001\u589e\u52a0\uff1a\u4f7f\u7528\u53ef\u53d8\u957f\u5ea6\u96c6\u5408\u7c7b arraylist\u4f7f\u7528 arrays.copyof() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\u4f7f\u7528 system.arraycopy() \u65b9\u6cd5\u590d\u5236\u5143\u7d20\u5230\u65b0\u6570\u7ec4\u91cd\u65b0\u5206\u914d\u6570\u7ec4\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111313252366291.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u4f7f\u6570\u7ec4\u957f\u5ea6\u589e\u52a0\u63d2\u56fe\" alt=\"java\u600e\u4e48\u4f7f\u6570\u7ec4\u957f\u5ea6\u589e\u52a0\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u52a8\u6001\u589e\u52a0 Java \u6570\u7ec4\u957f\u5ea6<\/strong><\/p>\n<p>Java \u6570\u7ec4\u5728\u521b\u5efa\u540e\u5177\u6709\u56fa\u5b9a\u957f\u5ea6\uff0c\u65e0\u6cd5\u52a8\u6001\u8c03\u6574\u5176\u5927\u5c0f\u3002\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6570\u7ec4\u957f\u5ea6\u7684\u589e\u52a0\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 ArrayList<\/strong><\/p>\n<p>ArrayList \u662f\u4e00\u4e2a\u53ef\u53d8\u957f\u5ea6\u7684\u96c6\u5408\u7c7b\uff0c\u652f\u6301\u52a8\u6001\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u3002\u53ef\u4ee5\u4f7f\u7528 ArrayList \u6765\u5b58\u50a8\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u6839\u636e\u9700\u8981\u81ea\u52a8\u589e\u52a0\u5176\u5bb9\u91cf\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();\nfor (int i = 0; i &lt; 10; i++) {\n  list.add(i);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Arrays.copyOf()<\/strong><\/p>\n<p>Arrays.copyOf() \u65b9\u6cd5\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u5927\u5c0f\u4e0e\u7ed9\u5b9a\u6570\u7ec4\u76f8\u540c\u6216\u66f4\u5927\u3002\u53ef\u4ee5\u901a\u8fc7\u5c06\u65b0\u7684\u5927\u5c0f\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u9012\u6765\u589e\u52a0\u6570\u7ec4\u957f\u5ea6\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3};\nint[] newArr = Arrays.copyOf(arr, 5);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 System.arraycopy()<\/strong><\/p>\n<p>System.arraycopy() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\u5e76\u4f7f\u7528 System.arraycopy() \u5c06\u73b0\u6709\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u590d\u5236\u5230\u5176\u4e2d\u6765\u589e\u52a0\u6570\u7ec4\u957f\u5ea6\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3};\nint[] newArr = new int[5];\nSystem.arraycopy(arr, 0, newArr, 0, arr.length);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u91cd\u65b0\u5206\u914d\u6570\u7ec4<\/strong><\/p>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u91cd\u65b0\u5206\u914d\u6570\u7ec4\u53ef\u80fd\u662f\u5fc5\u8981\u7684\u3002\u8fd9\u6d89\u53ca\u521b\u5efa\u4e00\u4e2a\u5c0f\u6570\u7ec4\uff0c\u5c06\u73b0\u6709\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u590d\u5236\u5230\u5176\u4e2d\uff0c\u7136\u540e\u5c06\u65b0\u6570\u7ec4\u5206\u914d\u7ed9\u539f\u59cb\u53d8\u91cf\u3002<\/p>\n<pre>int[] arr = {1, 2, 3};\narr = new int[5];\narr[0] = 1;\narr[1] = 2;\narr[2] = 3;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u52a8\u6001\u589e\u52a0 Java \u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4ee5\u6ee1\u8db3\u60a8\u7684\u7279\u5b9a\u9700\u6c42\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u4f7f\u6570\u7ec4\u957f\u5ea6\u589e\u52a0\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 \u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u52a8\u6001\u589e\u52a0\uff1a\u4f7f\u7528\u53ef\u53d8\u957f\u5ea6\u96c6\u5408\u7c7b arraylist\u4f7f\u7528 arrays.copyof() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\u4f7f\u7528 system.arraycopy() \u65b9\u6cd5\u590d\u5236\u5143\u7d20\u5230\u65b0\u6570\u7ec4\u91cd\u65b0\u5206\u914d\u6570\u7ec4 \u5982\u4f55\u52a8\u6001\u589e\u52a0 Java \u6570\u7ec4\u957f\u5ea6 Java \u6570\u7ec4\u5728\u521b\u5efa\u540e\u5177\u6709\u56fa\u5b9a\u957f\u5ea6\uff0c\u65e0\u6cd5\u52a8\u6001\u8c03\u6574\u5176\u5927\u5c0f\u3002\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6570\u7ec4\u957f\u5ea6\u7684\u589e\u52a0\uff1a 1. \u4f7f\u7528 ArrayList ArrayList \u662f\u4e00\u4e2a\u53ef\u53d8\u957f\u5ea6\u7684\u96c6\u5408\u7c7b\uff0c\u652f\u6301\u52a8\u6001\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u3002\u53ef\u4ee5\u4f7f\u7528 ArrayList \u6765\u5b58\u50a8\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u6839\u636e\u9700\u8981\u81ea\u52a8\u589e\u52a0\u5176\u5bb9\u91cf\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;(); for (int i = 0; i &lt; 10; i++) { list.add(i); } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 Arrays.copyOf() Arrays.copyOf() \u65b9\u6cd5\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u5927\u5c0f\u4e0e\u7ed9\u5b9a\u6570\u7ec4\u76f8\u540c\u6216\u66f4\u5927\u3002\u53ef\u4ee5\u901a\u8fc7\u5c06\u65b0\u7684\u5927\u5c0f\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u9012\u6765\u589e\u52a0\u6570\u7ec4\u957f\u5ea6\uff1a int[] arr = {1, 2, 3}; int[] newArr = Arrays.copyOf(arr, 5); \u767b\u5f55\u540e\u590d\u5236 [&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-37933","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37933","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=37933"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37933\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}