{"id":34733,"date":"2024-11-26T10:04:34","date_gmt":"2024-11-26T02:04:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/34733\/"},"modified":"2024-11-26T10:04:34","modified_gmt":"2024-11-26T02:04:34","slug":"java%e9%87%8c%e4%b8%80%e7%bb%b4%e6%95%b0%e7%bb%84%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\/34733\/","title":{"rendered":"java\u91cc\u4e00\u7ef4\u6570\u7ec4\u600e\u4e48\u5220\u9664\u5143\u7d20"},"content":{"rendered":"<blockquote><p>\n  java\u4e2d\u4e00\u7ef4\u6570\u7ec4\u5220\u9664\u5143\u7d20\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4e00\u3001\u4f7f\u7528system.arraycopy()\u590d\u5236\u6570\u7ec4\uff0c\u4ece\u5220\u9664\u5143\u7d20\u5904\u5f00\u59cb\uff1b\u4e8c\u3001\u8f6c\u6362\u4e3aarraylist\uff0c\u4f7f\u7528arraylist.remove()\u5220\u9664\u5143\u7d20\uff0c\u518d\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1b\u4e09\u3001\u5faa\u73af\u904d\u5386\uff0c\u9047\u5230\u5220\u9664\u5143\u7d20\u5219\u8df3\u8fc7\uff0c\u5e76\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110302061832283.jpg\" class=\"aligncenter\" title=\"java\u91cc\u4e00\u7ef4\u6570\u7ec4\u600e\u4e48\u5220\u9664\u5143\u7d20\u63d2\u56fe\" alt=\"java\u91cc\u4e00\u7ef4\u6570\u7ec4\u600e\u4e48\u5220\u9664\u5143\u7d20\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u4e2d\u4e00\u7ef4\u6570\u7ec4\u5220\u9664\u5143\u7d20<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u5220\u9664<\/strong><\/p>\n<p>\u4f7f\u7528System.arraycopy()\u65b9\u6cd5\u590d\u5236\u6570\u7ec4\uff0c\u4ece\u5220\u9664\u5143\u7d20\u7684\u7d22\u5f15\u5904\u5f00\u59cb\u590d\u5236\u3002<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint removeIndex = 2;\n\n\/\/ \u590d\u5236\u6570\u7ec4\uff0c\u4ece\u5220\u9664\u5143\u7d20\u7684\u7d22\u5f15\u5904\u5f00\u59cb\nint[] newArr = Arrays.copyOfRange(arr, 0, removeIndex);\nnewArr = Arrays.copyOfRange(arr, removeIndex + 1, arr.length);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528ArrayList<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aArrayList\uff0c\u4f7f\u7528ArrayList.remove()\u65b9\u6cd5\u5220\u9664\u5143\u7d20\uff0c\u7136\u540e\u518d\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint removeIndex = 2;\n\n\/\/ \u8f6c\u6362\u4e3aArrayList\nArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;(Arrays.asList(arr));\n\n\/\/ \u5220\u9664\u5143\u7d20\nlist.remove(removeIndex);\n\n\/\/ \u8f6c\u6362\u4e3a\u6570\u7ec4\nint[] newArr = list.stream()\n                    .mapToInt(Integer::intValue)\n                    .toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5faa\u73af\u5220\u9664<\/strong><\/p>\n<p>\u904d\u5386\u6570\u7ec4\uff0c\u5f53\u9047\u5230\u8981\u5220\u9664\u7684\u5143\u7d20\u65f6\u5c06\u5176\u8df3\u8fc7\u5e76\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint removeIndex = 2;\n\n\/\/ \u521b\u5efa\u65b0\u6570\u7ec4\nint[] newArr = new int[arr.length - 1];\n\n\/\/ \u904d\u5386\u6570\u7ec4\nfor (int i = 0, j = 0; i &lt; arr.length; i++) {\n    if (i == removeIndex) {\n        continue;\n    }\n    newArr[j++] = arr[i];\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u91cc\u4e00\u7ef4\u6570\u7ec4\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\u4e2d\u4e00\u7ef4\u6570\u7ec4\u5220\u9664\u5143\u7d20\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4e00\u3001\u4f7f\u7528system.arraycopy()\u590d\u5236\u6570\u7ec4\uff0c\u4ece\u5220\u9664\u5143\u7d20\u5904\u5f00\u59cb\uff1b\u4e8c\u3001\u8f6c\u6362\u4e3aarraylist\uff0c\u4f7f\u7528arraylist.remove()\u5220\u9664\u5143\u7d20\uff0c\u518d\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1b\u4e09\u3001\u5faa\u73af\u904d\u5386\uff0c\u9047\u5230\u5220\u9664\u5143\u7d20\u5219\u8df3\u8fc7\uff0c\u5e76\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002 Java\u4e2d\u4e00\u7ef4\u6570\u7ec4\u5220\u9664\u5143\u7d20 \u76f4\u63a5\u5220\u9664 \u4f7f\u7528System.arraycopy()\u65b9\u6cd5\u590d\u5236\u6570\u7ec4\uff0c\u4ece\u5220\u9664\u5143\u7d20\u7684\u7d22\u5f15\u5904\u5f00\u59cb\u590d\u5236\u3002 int[] arr = {1, 2, 3, 4, 5}; int removeIndex = 2; \/\/ \u590d\u5236\u6570\u7ec4\uff0c\u4ece\u5220\u9664\u5143\u7d20\u7684\u7d22\u5f15\u5904\u5f00\u59cb int[] newArr = Arrays.copyOfRange(arr, 0, removeIndex); newArr = Arrays.copyOfRange(arr, removeIndex + 1, arr.length); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528ArrayList \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aArrayList\uff0c\u4f7f\u7528ArrayList.remove()\u65b9\u6cd5\u5220\u9664\u5143\u7d20\uff0c\u7136\u540e\u518d\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002 int[] arr = {1, 2, 3, 4, 5}; int removeIndex = 2; \/\/ \u8f6c\u6362\u4e3aArrayList ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;(Arrays.asList(arr)); \/\/ [&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-34733","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34733","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=34733"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34733\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=34733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=34733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=34733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}