{"id":36837,"date":"2024-11-26T15:29:00","date_gmt":"2024-11-26T07:29:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36837\/"},"modified":"2024-11-26T15:29:00","modified_gmt":"2024-11-26T07:29:00","slug":"java%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%8a%a0%e6%88%90%e5%91%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36837\/","title":{"rendered":"java\u6570\u7ec4\u600e\u4e48\u52a0\u6210\u5458"},"content":{"rendered":"<blockquote><p>\n  java \u6570\u7ec4\u4e00\u65e6\u521b\u5efa\uff0c\u5176\u957f\u5ea6\u65e0\u6cd5\u8c03\u6574\u3002\u8981\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u6210\u5458\uff0c\u6709\u4ee5\u4e0b 2 \u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 system.arraycopy \u65b9\u6cd5\u590d\u5236\u65e7\u6570\u7ec4\u5230\u65b0\u6570\u7ec4\uff0c\u5e76\u6dfb\u52a0\u65b0\u6210\u5458\u3002\u4f7f\u7528 java 8 \u4e2d\u7684 arrays.copyof \u65b9\u6cd5\u590d\u5236\u65e7\u6570\u7ec4\u5230\u65b0\u6570\u7ec4\uff0c\u5e76\u6dfb\u52a0\u65b0\u6210\u5458\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111220040162351.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u600e\u4e48\u52a0\u6210\u5458\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u600e\u4e48\u52a0\u6210\u5458\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\u5982\u4f55\u6dfb\u52a0\u6210\u5458<\/strong><\/p>\n<p>Java \u6570\u7ec4\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7531\u76f8\u540c\u7c7b\u578b\u5143\u7d20\u7684\u4e00\u7ec4\u6709\u5e8f\u96c6\u5408\u7ec4\u6210\u3002\u4e00\u65e6\u521b\u5efa\uff0c\u5b83\u7684\u957f\u5ea6\u5c06\u88ab\u56fa\u5b9a\uff0c\u4f46\u4e0d\u80fd\u5c06\u5176\u5927\u5c0f\u52a8\u6001\u8c03\u6574\u3002<\/p>\n<p>\u8981\u5411\u73b0\u6709\u7684 Java \u6570\u7ec4\u4e2d\u6dfb\u52a0\u65b0\u6210\u5458\uff0c\u6709\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 System.arraycopy \u65b9\u6cd5<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>System.arraycopy \u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6570\u7ec4\u7684\u6307\u5b9a\u90e8\u5206\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002\u5b83\u5177\u6709\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c<\/p>\n<ul>\n<li> <strong>src<\/strong> \u662f\u8981\u590d\u5236\u7684\u6e90\u6570\u7ec4\u3002<\/li>\n<li> <strong>srcPos<\/strong> \u662f\u8981\u590d\u5236\u7684\u6e90\u6570\u7ec4\u4e2d\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002<\/li>\n<li> <strong>dest<\/strong> \u662f\u8981\u590d\u5236\u7684\u76ee\u6807\u6570\u7ec4\u3002<\/li>\n<li> <strong>destPos<\/strong> \u662f\u8981\u590d\u5236\u5230\u76ee\u6807\u6570\u7ec4\u4e2d\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002<\/li>\n<li> <strong>length<\/strong> \u662f\u8981\u590d\u5236\u7684\u5143\u7d20\u6570\u3002<\/li>\n<\/ul>\n<p>\u8981\u4f7f\u7528\u6b64\u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u6210\u5458\uff0c\u53ef\u4ee5\u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u5728\u65e7\u6570\u7ec4\u4e2d\u590d\u5236\u6240\u6709\u5143\u7d20\uff0c\u7136\u540e\u5c06\u65b0\u6210\u5458\u6dfb\u52a0\u5230\u65b0\u6570\u7ec4\u7684\u672b\u5c3e\u3002<\/p>\n<pre>int[] oldArray = {1, 2, 3};\nint newArrayLength = oldArray.length + 1;\nint[] newArray = new int[newArrayLength];\nSystem.arraycopy(oldArray, 0, newArray, 0, oldArray.length);\nnewArray[newArrayLength - 1] = 4;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u73b0\u5728 newArray \u4e2d\u5305\u542b\u4e86 oldArray \u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u4ee5\u53ca\u65b0\u6dfb\u52a0\u7684\u6210\u5458 4\u3002<\/p>\n<p><strong>2. \u4f7f\u7528 Java 8 \u4e2d\u7684 Arrays.copyOf \u65b9\u6cd5<\/strong><\/p>\n<p>Arrays.copyOf \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5b83\u5305\u542b\u6307\u5b9a\u539f\u59cb\u6570\u7ec4\u7684\u6307\u5b9a\u90e8\u5206\u3002\u5b83\u5177\u6709\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>public static &lt;T&gt; T[] copyOf(T[] original, int newLength)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c<\/p>\n<ul>\n<li> <strong>original<\/strong> \u662f\u8981\u590d\u5236\u7684\u539f\u59cb\u6570\u7ec4\u3002<\/li>\n<li> <strong>newLength<\/strong> \u662f\u65b0\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/li>\n<\/ul>\n<p>\u8981\u4f7f\u7528\u6b64\u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u6210\u5458\uff0c\u53ef\u4ee5\u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u5728\u65e7\u6570\u7ec4\u4e2d\u590d\u5236\u6240\u6709\u5143\u7d20\uff0c\u7136\u540e\u5c06\u65b0\u6210\u5458\u6dfb\u52a0\u5230\u65b0\u6570\u7ec4\u7684\u672b\u5c3e\u3002<\/p>\n<pre>int[] oldArray = {1, 2, 3};\nint newArrayLength = oldArray.length + 1;\nint[] newArray = Arrays.copyOf(oldArray, newArrayLength);\nnewArray[newArrayLength - 1] = 4;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e0e\u4f7f\u7528 System.arraycopy \u65b9\u6cd5\u76f8\u540c\uff0c\u4f46\u66f4\u7b80\u6d01\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u600e\u4e48\u52a0\u6210\u5458\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\u4e00\u65e6\u521b\u5efa\uff0c\u5176\u957f\u5ea6\u65e0\u6cd5\u8c03\u6574\u3002\u8981\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u6210\u5458\uff0c\u6709\u4ee5\u4e0b 2 \u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 system.arraycopy \u65b9\u6cd5\u590d\u5236\u65e7\u6570\u7ec4\u5230\u65b0\u6570\u7ec4\uff0c\u5e76\u6dfb\u52a0\u65b0\u6210\u5458\u3002\u4f7f\u7528 java 8 \u4e2d\u7684 arrays.copyof \u65b9\u6cd5\u590d\u5236\u65e7\u6570\u7ec4\u5230\u65b0\u6570\u7ec4\uff0c\u5e76\u6dfb\u52a0\u65b0\u6210\u5458\u3002 Java \u6570\u7ec4\u5982\u4f55\u6dfb\u52a0\u6210\u5458 Java \u6570\u7ec4\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7531\u76f8\u540c\u7c7b\u578b\u5143\u7d20\u7684\u4e00\u7ec4\u6709\u5e8f\u96c6\u5408\u7ec4\u6210\u3002\u4e00\u65e6\u521b\u5efa\uff0c\u5b83\u7684\u957f\u5ea6\u5c06\u88ab\u56fa\u5b9a\uff0c\u4f46\u4e0d\u80fd\u5c06\u5176\u5927\u5c0f\u52a8\u6001\u8c03\u6574\u3002 \u8981\u5411\u73b0\u6709\u7684 Java \u6570\u7ec4\u4e2d\u6dfb\u52a0\u65b0\u6210\u5458\uff0c\u6709\u4ee5\u4e0b\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 System.arraycopy \u65b9\u6cd5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b System.arraycopy \u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6570\u7ec4\u7684\u6307\u5b9a\u90e8\u5206\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002\u5b83\u5177\u6709\u4ee5\u4e0b\u8bed\u6cd5\uff1a public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0c src \u662f\u8981\u590d\u5236\u7684\u6e90\u6570\u7ec4\u3002 srcPos \u662f\u8981\u590d\u5236\u7684\u6e90\u6570\u7ec4\u4e2d\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002 dest \u662f\u8981\u590d\u5236\u7684\u76ee\u6807\u6570\u7ec4\u3002 destPos \u662f\u8981\u590d\u5236\u5230\u76ee\u6807\u6570\u7ec4\u4e2d\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002 length \u662f\u8981\u590d\u5236\u7684\u5143\u7d20\u6570\u3002 \u8981\u4f7f\u7528\u6b64\u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u6210\u5458\uff0c\u53ef\u4ee5\u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u5728\u65e7\u6570\u7ec4\u4e2d\u590d\u5236\u6240\u6709\u5143\u7d20\uff0c\u7136\u540e\u5c06\u65b0\u6210\u5458\u6dfb\u52a0\u5230\u65b0\u6570\u7ec4\u7684\u672b\u5c3e\u3002 int[] oldArray = {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-36837","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36837","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=36837"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36837\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}