{"id":38197,"date":"2024-11-26T13:49:38","date_gmt":"2024-11-26T05:49:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38197\/"},"modified":"2024-11-26T13:49:38","modified_gmt":"2024-11-26T05:49:38","slug":"java%e4%b8%ad%e6%80%8e%e4%b9%88%e5%88%a0%e9%99%a4%e6%95%b0%e7%bb%84%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38197\/","title":{"rendered":"java\u4e2d\u600e\u4e48\u5220\u9664\u6570\u7ec4\u5143\u7d20"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u5220\u9664\u6570\u7ec4\u5143\u7d20\u6709\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a\u4f7f\u7528 system.arraycopy() \u590d\u5236\u5143\u7d20\uff0c\u6709\u6548\u5220\u9664\u76ee\u6807\u5143\u7d20\u3002\u4f7f\u7528 arrays.copyof() \u521b\u5efa\u8f83\u77ed\u7684\u526f\u672c\uff0c\u4e0d\u5305\u542b\u76ee\u6807\u5143\u7d20\u3002\u4f7f\u7528 arraylist \u5b58\u50a8\u5143\u7d20\uff0c\u5e76\u4f7f\u7528 remove() \u65b9\u6cd5\u5220\u9664\u7279\u5b9a\u5143\u7d20\uff0c\u4e0d\u4fee\u6539\u539f\u59cb\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111410331997523.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u600e\u4e48\u5220\u9664\u6570\u7ec4\u5143\u7d20\u63d2\u56fe\" alt=\"java\u4e2d\u600e\u4e48\u5220\u9664\u6570\u7ec4\u5143\u7d20\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u5220\u9664\u6570\u7ec4\u5143\u7d20<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5220\u9664\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>1. \u4f7f\u7528 System.arraycopy()<\/strong><\/p>\n<p><em>System.arraycopy()<\/em> \u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u90e8\u5206\u6216\u5168\u90e8\u5185\u5bb9\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002\u901a\u8fc7\u5c06\u8981\u5220\u9664\u7684\u5143\u7d20\u540e\u9762\u7684\u5143\u7d20\u590d\u5236\u5230\u8981\u5220\u9664\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5220\u9664\u8be5\u5143\u7d20\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint indexToRemove = 2;\n\nSystem.arraycopy(arr, indexToRemove + 1, arr, indexToRemove, arr.length - indexToRemove - 1);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Arrays.copyOf()<\/strong><\/p>\n<p><em>Arrays.copyOf()<\/em> \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u662f\u539f\u59cb\u6570\u7ec4\u7684\u526f\u672c\uff0c\u4f46\u957f\u5ea6\u4e0d\u540c\u3002\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u4e00\u4e2a\u8f83\u77ed\u7684\u957f\u5ea6\u6765\u521b\u5efa\u4e00\u4e2a\u4e0d\u5305\u542b\u8981\u5220\u9664\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint indexToRemove = 2;\n\nint[] newArr = Arrays.copyOf(arr, arr.length - 1);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 ArrayList<\/strong><\/p>\n<p>\u5982\u679c\u4e0d\u60f3\u4fee\u6539\u539f\u59cb\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528 <em>ArrayList<\/em>\u3002<em>ArrayList<\/em> \u662f\u4e00\u4e2a\u5b58\u50a8\u5143\u7d20\u7684\u52a8\u6001\u5927\u5c0f\u7684\u5217\u8868\uff0c\u5b83\u5141\u8bb8\u63d2\u5165\u3001\u5220\u9664\u548c\u67e5\u627e\u5143\u7d20\u3002<\/p>\n<pre>List&lt;Integer&gt; list = Arrays.asList(1, 2, 3, 4, 5);\nlist.remove(indexToRemove);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u524d\u4e24\u79cd\u65b9\u6cd5\u90fd\u4f1a\u4fee\u6539\u539f\u59cb\u6570\u7ec4\u3002<\/li>\n<li>\u7b2c\u4e09\u79cd\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 <em>ArrayList<\/em>\uff0c\u800c\u4e0d\u4f1a\u4fee\u6539\u539f\u59cb\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u600e\u4e48\u5220\u9664\u6570\u7ec4\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\uff0c\u5220\u9664\u6570\u7ec4\u5143\u7d20\u6709\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a\u4f7f\u7528 system.arraycopy() \u590d\u5236\u5143\u7d20\uff0c\u6709\u6548\u5220\u9664\u76ee\u6807\u5143\u7d20\u3002\u4f7f\u7528 arrays.copyof() \u521b\u5efa\u8f83\u77ed\u7684\u526f\u672c\uff0c\u4e0d\u5305\u542b\u76ee\u6807\u5143\u7d20\u3002\u4f7f\u7528 arraylist \u5b58\u50a8\u5143\u7d20\uff0c\u5e76\u4f7f\u7528 remove() \u65b9\u6cd5\u5220\u9664\u7279\u5b9a\u5143\u7d20\uff0c\u4e0d\u4fee\u6539\u539f\u59cb\u6570\u7ec4\u3002 Java \u4e2d\u5220\u9664\u6570\u7ec4\u5143\u7d20 \u5728 Java \u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5220\u9664\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002 1. \u4f7f\u7528 System.arraycopy() System.arraycopy() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u90e8\u5206\u6216\u5168\u90e8\u5185\u5bb9\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002\u901a\u8fc7\u5c06\u8981\u5220\u9664\u7684\u5143\u7d20\u540e\u9762\u7684\u5143\u7d20\u590d\u5236\u5230\u8981\u5220\u9664\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5220\u9664\u8be5\u5143\u7d20\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] arr = {1, 2, 3, 4, 5}; int indexToRemove = 2; System.arraycopy(arr, indexToRemove + 1, arr, indexToRemove, arr.length &#8211; indexToRemove &#8211; 1); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 Arrays.copyOf() Arrays.copyOf() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u662f\u539f\u59cb\u6570\u7ec4\u7684\u526f\u672c\uff0c\u4f46\u957f\u5ea6\u4e0d\u540c\u3002\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u4e00\u4e2a\u8f83\u77ed\u7684\u957f\u5ea6\u6765\u521b\u5efa\u4e00\u4e2a\u4e0d\u5305\u542b\u8981\u5220\u9664\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002 int[] arr = {1, [&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-38197","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38197","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=38197"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38197\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}