{"id":37388,"date":"2024-11-26T10:52:41","date_gmt":"2024-11-26T02:52:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37388\/"},"modified":"2024-11-26T10:52:41","modified_gmt":"2024-11-26T02:52:41","slug":"java%e6%95%b0%e7%bb%84%e4%b8%ad%e6%80%8e%e4%b9%88%e5%88%a0%e9%99%a4%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37388\/","title":{"rendered":"java\u6570\u7ec4\u4e2d\u600e\u4e48\u5220\u9664\u5143\u7d20"},"content":{"rendered":"<blockquote><p>\n  java \u6570\u7ec4\u4e2d\u5220\u9664\u5143\u7d20\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u91cd\u65b0\u521b\u5efa\u6570\u7ec4\uff0c\u957f\u5ea6\u51cf\u53bb\u8981\u5220\u9664\u7684\u5143\u7d20\u6570\uff0c\u5c06\u4fdd\u7559\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\u4f7f\u7528 system.arraycopy() \u79fb\u52a8\u8981\u5220\u9664\u7684\u5143\u7d20\u540e\u7684\u5143\u7d20\u5230\u8981\u4fdd\u7559\u7684\u5143\u7d20\u540e\u9762\u3002\u8f6c\u6362\u4e3a arraylist\uff0c\u4ece\u5176\u4e2d\u5220\u9664\u5143\u7d20\uff0c\u518d\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111304543927505.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u4e2d\u600e\u4e48\u5220\u9664\u5143\u7d20\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u4e2d\u600e\u4e48\u5220\u9664\u5143\u7d20\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\u4e2d\u5220\u9664\u5143\u7d20<\/strong><\/p>\n<p>\u8981\u4ece Java \u6570\u7ec4\u4e2d\u5220\u9664\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u91cd\u65b0\u521b\u5efa\u6570\u7ec4<\/strong><\/p>\n<p>\u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u5c06\u5176\u957f\u5ea6\u8bbe\u7f6e\u4e3a\u539f\u59cb\u6570\u7ec4\u7684\u957f\u5ea6\u51cf\u53bb\u8981\u5220\u9664\u7684\u5143\u7d20\u6570\u3002\u7136\u540e\u5c06\u539f\u59cb\u6570\u7ec4\u4e2d\u8981\u4fdd\u7559\u7684\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint indexToRemove = 2;\n\n\/\/ \u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u957f\u5ea6\u51cf\u53bb\u8981\u5220\u9664\u7684\u5143\u7d20\u6570\nint[] newArr = new int[arr.length - 1];\n\n\/\/ \u590d\u5236\u8981\u4fdd\u7559\u7684\u5143\u7d20\nfor (int i = 0; i &lt; indexToRemove; i++) {\n    newArr[i] = arr[i];\n}\nfor (int i = indexToRemove + 1; i &lt; arr.length; i++) {\n    newArr[i - 1] = arr[i];\n}\n\n\/\/ \u6307\u5411\u65b0\u6570\u7ec4\narr = newArr;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 System.arraycopy()<\/strong><\/p>\n<p>\u4f7f\u7528 System.arraycopy() \u65b9\u6cd5\u5c06\u539f\u59cb\u6570\u7ec4\u4e2d\u8981\u5220\u9664\u7684\u5143\u7d20\u540e\u7684\u5143\u7d20\u79fb\u52a8\u5230\u8981\u4fdd\u7559\u7684\u5143\u7d20\u540e\u9762\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint indexToRemove = 2;\n\n\/\/ \u8ba1\u7b97\u8981\u79fb\u52a8\u7684\u5143\u7d20\u6570\u91cf\nint numElementsToMove = arr.length - indexToRemove - 1;\n\n\/\/ \u79fb\u52a8\u5143\u7d20\nSystem.arraycopy(arr, indexToRemove + 1, arr, indexToRemove, numElementsToMove);\n\n\/\/ \u5c06\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u8bbe\u7f6e\u4e3a\u7a7a\narr[arr.length - 1] = 0;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 ArrayList<\/strong><\/p>\n<p>\u5c06 Java \u6570\u7ec4\u8f6c\u6362\u4e3a ArrayList\uff0c\u4ece\u5176\u4e2d\u5220\u9664\u5143\u7d20\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint indexToRemove = 2;\n\n\/\/ \u8f6c\u6362\u4e3a ArrayList\nArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();\nfor (int i : arr) {\n    list.add(i);\n}\n\n\/\/ \u4ece ArrayList \u4e2d\u5220\u9664\u5143\u7d20\nlist.remove(indexToRemove);\n\n\/\/ \u8f6c\u6362\u4e3a\u6570\u7ec4\narr = list.stream().mapToInt(Integer::intValue).toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u4e2d\u600e\u4e48\u5220\u9664\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>java \u6570\u7ec4\u4e2d\u5220\u9664\u5143\u7d20\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u91cd\u65b0\u521b\u5efa\u6570\u7ec4\uff0c\u957f\u5ea6\u51cf\u53bb\u8981\u5220\u9664\u7684\u5143\u7d20\u6570\uff0c\u5c06\u4fdd\u7559\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\u4f7f\u7528 system.arraycopy() \u79fb\u52a8\u8981\u5220\u9664\u7684\u5143\u7d20\u540e\u7684\u5143\u7d20\u5230\u8981\u4fdd\u7559\u7684\u5143\u7d20\u540e\u9762\u3002\u8f6c\u6362\u4e3a arraylist\uff0c\u4ece\u5176\u4e2d\u5220\u9664\u5143\u7d20\uff0c\u518d\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002 Java \u6570\u7ec4\u4e2d\u5220\u9664\u5143\u7d20 \u8981\u4ece Java \u6570\u7ec4\u4e2d\u5220\u9664\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a 1. \u91cd\u65b0\u521b\u5efa\u6570\u7ec4 \u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u5c06\u5176\u957f\u5ea6\u8bbe\u7f6e\u4e3a\u539f\u59cb\u6570\u7ec4\u7684\u957f\u5ea6\u51cf\u53bb\u8981\u5220\u9664\u7684\u5143\u7d20\u6570\u3002\u7136\u540e\u5c06\u539f\u59cb\u6570\u7ec4\u4e2d\u8981\u4fdd\u7559\u7684\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u793a\u4f8b\uff1a int[] arr = {1, 2, 3, 4, 5}; int indexToRemove = 2; \/\/ \u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u957f\u5ea6\u51cf\u53bb\u8981\u5220\u9664\u7684\u5143\u7d20\u6570 int[] newArr = new int[arr.length &#8211; 1]; \/\/ \u590d\u5236\u8981\u4fdd\u7559\u7684\u5143\u7d20 for (int i = 0; i &lt; indexToRemove; i++) { newArr[i] = arr[i]; } for (int i [&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-37388","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37388","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=37388"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37388\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}