{"id":39395,"date":"2024-11-26T17:32:52","date_gmt":"2024-11-26T09:32:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39395\/"},"modified":"2024-11-26T17:32:52","modified_gmt":"2024-11-26T09:32:52","slug":"java-%e6%80%8e%e4%b9%88%e6%89%b9%e9%87%8f%e5%88%a0%e9%99%a4%e6%95%b0%e7%bb%84%e7%9a%84%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39395\/","title":{"rendered":"java \u600e\u4e48\u6279\u91cf\u5220\u9664\u6570\u7ec4\u7684\u5143\u7d20"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u6279\u91cf\u5220\u9664\u6570\u7ec4\u5143\u7d20\uff0c\u6709\u4e94\u79cd\u65b9\u6cd5\uff1a\u76f4\u63a5\u8d4b\u503c\u4e3a\u65b0\u6570\u7ec4\u3001system.arraycopy()\u3001arrays.copyof()\u3001arrays.copyofrange() \u548c\u4f7f\u7528 arraylist\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111607132061535.jpg\" class=\"aligncenter\" title=\"java \u600e\u4e48\u6279\u91cf\u5220\u9664\u6570\u7ec4\u7684\u5143\u7d20\u63d2\u56fe\" alt=\"java \u600e\u4e48\u6279\u91cf\u5220\u9664\u6570\u7ec4\u7684\u5143\u7d20\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u6279\u91cf\u5220\u9664 Java \u4e2d\u6570\u7ec4\u7684\u5143\u7d20<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u8d4b\u503c\u4e3a\u4e00\u4e2a\u65b0\u6570\u7ec4<\/strong><\/p>\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5c06\u6570\u7ec4\u76f4\u63a5\u8d4b\u503c\u4e3a\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u53ea\u5305\u542b\u6240\u9700\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint[] newArr = {1, 3, 5};\narr = newArr;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 System.arraycopy()<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>System.arraycopy() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\u3002\u901a\u8fc7\u5c06\u9700\u8981\u4fdd\u7559\u7684\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5220\u9664\u4e0d\u9700\u8981\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint[] newArr = new int[3];\nSystem.arraycopy(arr, 0, newArr, 0, 3);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Arrays.copyOf()<\/strong><\/p>\n<p>Arrays.copyOf() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u6307\u5b9a\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\uff0c\u5305\u542b\u539f\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u3002\u8fd9\u4e0e System.arraycopy() \u7c7b\u4f3c\uff0c\u4f46\u4e0d\u9700\u8981\u6307\u5b9a\u6e90\u6570\u7ec4\u548c\u76ee\u6807\u6570\u7ec4\u7684\u504f\u79fb\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint[] newArr = Arrays.copyOf(arr, 3);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Arrays.copyOfRange()<\/strong><\/p>\n<p>Arrays.copyOfRange() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u7684\u65b0\u6570\u7ec4\uff0c\u5305\u542b\u539f\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u3002\u8fd9\u5141\u8bb8\u6309\u7d22\u5f15\u6307\u5b9a\u8981\u4fdd\u7559\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint[] newArr = Arrays.copyOfRange(arr, 1, 4);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 ArrayList<\/strong><\/p>\n<p>Java \u4e2d\u7684 ArrayList \u662f\u4e00\u4e2a\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u3002\u5b83\u63d0\u4f9b\u4e86 remove() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u6309\u7d22\u5f15\u6216\u5bf9\u8c61\u4ece\u5217\u8868\u4e2d\u5220\u9664\u5143\u7d20\u3002\u901a\u8fc7\u5c06\u6570\u7ec4\u5143\u7d20\u6dfb\u52a0\u5230 ArrayList \u4e2d\u5e76\u5220\u9664\u4e0d\u9700\u8981\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();\nfor (int i : arr) {\n    list.add(i);\n}\nlist.remove(1); \/\/ \u5220\u9664\u7d22\u5f15\u4e3a 1 \u7684\u5143\u7d20 (2)\nint[] newArr = new int[list.size()];\nfor (int i = 0; i &lt; list.size(); i++) {\n    newArr[i] = list.get(i);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava \u600e\u4e48\u6279\u91cf\u5220\u9664\u6570\u7ec4\u7684\u5143\u7d20\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>\u5728 java \u4e2d\u6279\u91cf\u5220\u9664\u6570\u7ec4\u5143\u7d20\uff0c\u6709\u4e94\u79cd\u65b9\u6cd5\uff1a\u76f4\u63a5\u8d4b\u503c\u4e3a\u65b0\u6570\u7ec4\u3001system.arraycopy()\u3001arrays.copyof()\u3001arrays.copyofrange() \u548c\u4f7f\u7528 arraylist\u3002 \u5982\u4f55\u6279\u91cf\u5220\u9664 Java \u4e2d\u6570\u7ec4\u7684\u5143\u7d20 \u76f4\u63a5\u8d4b\u503c\u4e3a\u4e00\u4e2a\u65b0\u6570\u7ec4 \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5c06\u6570\u7ec4\u76f4\u63a5\u8d4b\u503c\u4e3a\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u53ea\u5305\u542b\u6240\u9700\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a int[] arr = {1, 2, 3, 4, 5}; int[] newArr = {1, 3, 5}; arr = newArr; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 System.arraycopy() \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b System.arraycopy() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\u3002\u901a\u8fc7\u5c06\u9700\u8981\u4fdd\u7559\u7684\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5220\u9664\u4e0d\u9700\u8981\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a int[] arr = {1, 2, 3, 4, 5}; int[] newArr = new int[3]; System.arraycopy(arr, 0, newArr, 0, 3); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 Arrays.copyOf() Arrays.copyOf() [&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-39395","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39395","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=39395"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39395\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}