{"id":39095,"date":"2024-11-26T11:12:14","date_gmt":"2024-11-26T03:12:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39095\/"},"modified":"2024-11-26T11:12:14","modified_gmt":"2024-11-26T03:12:14","slug":"java%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%88%a0%e9%99%a4%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39095\/","title":{"rendered":"java\u6570\u7ec4\u600e\u4e48\u5220\u9664\u6570\u636e\u7ed3\u6784"},"content":{"rendered":"<blockquote><p>\n  java\u6570\u7ec4\u5220\u9664\u5143\u7d20\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528system.arraycopy()\u65b9\u6cd5\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\u4f7f\u7528arrays.copyofrange()\u65b9\u6cd5\u521b\u5efa\u65b0\u6570\u7ec4\u3002\u4f7f\u7528arraylist\u5b9e\u73b0\u5143\u7d20\u7684\u5feb\u901f\u6dfb\u52a0\u548c\u5220\u9664\u3002\u4f7f\u7528java 8 streams api\u8fc7\u6ee4\u5143\u7d20\u5e76\u521b\u5efa\u65b0\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111604002094138.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u600e\u4e48\u5220\u9664\u6570\u636e\u7ed3\u6784\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u600e\u4e48\u5220\u9664\u6570\u636e\u7ed3\u6784\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u6570\u7ec4\u5220\u9664\u5143\u7d20<\/strong><\/p>\n<p>\u5728Java\u6570\u7ec4\u4e2d\u5220\u9664\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528System.arraycopy()\u65b9\u6cd5<\/strong><\/p>\n<p>\u6b64\u65b9\u6cd5\u53ef\u7528\u4e8e\u5c06\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u4ece\u800c\u6709\u6548\u5730\u5220\u9664\u5143\u7d20\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] array = {1, 2, 3, 4, 5};\nint[] newArray = new int[array.length - 1];\nSystem.arraycopy(array, 0, newArray, 0, 2);\nSystem.arraycopy(array, 3, newArray, 2, 2);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528Arrays.copyOfRange()\u65b9\u6cd5<\/strong><\/p>\n<p>Java 8\u5f15\u5165\u4e86Arrays.copyOfRange()\u65b9\u6cd5\uff0c\u53ef\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u6307\u5b9a\u8303\u56f4\u7684\u5143\u7d20\u3002<\/p>\n<pre>int[] array = {1, 2, 3, 4, 5};\nint[] newArray = Arrays.copyOfRange(array, 0, 2);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528ArrayList<\/strong><\/p>\n<p>\u5982\u679c\u6570\u7ec4\u9700\u8981\u7ecf\u5e38\u4fee\u6539\uff0c\u53ef\u4ee5\u4f7f\u7528ArrayList\uff0c\u5b83\u6bd4\u6570\u7ec4\u66f4\u7075\u6d3b\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u3002<\/p>\n<pre>ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();\nlist.add(1);\nlist.add(2);\nlist.add(3);\nlist.add(4);\nlist.add(5);\nlist.remove(2); \/\/ \u5220\u9664\u7d22\u5f15\u4e3a2\u7684\u5143\u7d20<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u6d41<\/strong><\/p>\n<p>\u4f7f\u7528Java 8 Streams API\uff0c\u53ef\u4ee5\u8fc7\u6ee4\u5143\u7d20\u5e76\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6240\u9700\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002<\/p>\n<pre>int[] array = {1, 2, 3, 4, 5};\nint[] newArray = IntStream.of(array)\n        .filter(x -&gt; x % 2 != 0)\n        .toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u600e\u4e48\u5220\u9664\u6570\u636e\u7ed3\u6784\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\u5220\u9664\u5143\u7d20\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528system.arraycopy()\u65b9\u6cd5\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\u4f7f\u7528arrays.copyofrange()\u65b9\u6cd5\u521b\u5efa\u65b0\u6570\u7ec4\u3002\u4f7f\u7528arraylist\u5b9e\u73b0\u5143\u7d20\u7684\u5feb\u901f\u6dfb\u52a0\u548c\u5220\u9664\u3002\u4f7f\u7528java 8 streams api\u8fc7\u6ee4\u5143\u7d20\u5e76\u521b\u5efa\u65b0\u6570\u7ec4\u3002 Java\u6570\u7ec4\u5220\u9664\u5143\u7d20 \u5728Java\u6570\u7ec4\u4e2d\u5220\u9664\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff1a 1. \u4f7f\u7528System.arraycopy()\u65b9\u6cd5 \u6b64\u65b9\u6cd5\u53ef\u7528\u4e8e\u5c06\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u4ece\u800c\u6709\u6548\u5730\u5220\u9664\u5143\u7d20\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] array = {1, 2, 3, 4, 5}; int[] newArray = new int[array.length &#8211; 1]; System.arraycopy(array, 0, newArray, 0, 2); System.arraycopy(array, 3, newArray, 2, 2); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528Arrays.copyOfRange()\u65b9\u6cd5 Java 8\u5f15\u5165\u4e86Arrays.copyOfRange()\u65b9\u6cd5\uff0c\u53ef\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u6307\u5b9a\u8303\u56f4\u7684\u5143\u7d20\u3002 int[] array = {1, 2, 3, 4, 5}; int[] newArray = Arrays.copyOfRange(array, 0, 2); \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-39095","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39095","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=39095"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39095\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}