{"id":38865,"date":"2024-11-26T14:13:17","date_gmt":"2024-11-26T06:13:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38865\/"},"modified":"2024-11-26T14:13:17","modified_gmt":"2024-11-26T06:13:17","slug":"java%e6%80%8e%e4%b9%88%e5%b0%86%e5%85%83%e7%b4%a0%e5%90%88%e5%b9%b6%e6%88%90%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38865\/","title":{"rendered":"java\u600e\u4e48\u5c06\u5143\u7d20\u5408\u5e76\u6210\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u4f7f\u7528 arrays.copyof() \u65b9\u6cd5\u5c06\u591a\u4e2a\u5143\u7d20\u5408\u5e76\u6210\u6570\u7ec4\uff0c\u6b65\u9aa4\u5982\u4e0b\uff1a\u4f7f\u7528 arrays.copyof() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u4e3a\u539f\u59cb\u6570\u7ec4\u5143\u7d20\u6570\u91cf\u4e4b\u548c\u7684\u65b0\u6570\u7ec4\u3002\u4f7f\u7528 system.arraycopy() \u65b9\u6cd5\u5c06\u5176\u4ed6\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\u65b0\u6570\u7ec4\u5c06\u5305\u542b\u6240\u6709\u539f\u59cb\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111507310329244.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5c06\u5143\u7d20\u5408\u5e76\u6210\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5c06\u5143\u7d20\u5408\u5e76\u6210\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u5c06\u5143\u7d20\u5408\u5e76\u6210\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5\u5c06\u591a\u4e2a\u5143\u7d20\u5408\u5e76\u6210\u4e00\u4e2a\u6570\u7ec4\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u539f\u59cb\u6570\u7ec4\u548c\u4e00\u4e2a\u65b0\u6570\u7ec4\u7684\u5927\u5c0f\uff08\u5143\u7d20\u6570\u91cf\uff09\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u539f\u59cb\u6570\u7ec4\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>static &lt;T, U extends T&gt; T[] copyOf(T[] original, int newLength)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u53c2\u6570\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>original &#8211; \u8981\u5408\u5e76\u7684\u539f\u59cb\u6570\u7ec4<\/li>\n<li>newLength &#8211; \u65b0\u6570\u7ec4\u7684\u5927\u5c0f\uff08\u5143\u7d20\u6570\u91cf\uff09<\/li>\n<\/ul>\n<p><strong>\u8fd4\u56de\u503c\uff1a<\/strong><\/p>\n<p>\u4e00\u4e2a\u5305\u542b\u539f\u59cb\u6570\u7ec4\u5143\u7d20\u7684\u65b0\u6570\u7ec4<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u6574\u6570\u6570\u7ec4\uff1a<\/p>\n<pre>int[] arr1 = {1, 2, 3};\nint[] arr2 = {4, 5};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u8fd9\u4e24\u4e2a\u6570\u7ec4\u5408\u5e76\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff1a<\/p>\n<pre>int[] newArr = Arrays.copyOf(arr1, arr1.length + arr2.length);\nSystem.arraycopy(arr2, 0, newArr, arr1.length, arr2.length);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>Arrays.copyOf() \u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u5927\u5c0f\u4e3a arr1.length + arr2.length \u7684\u65b0\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528 System.arraycopy() \u65b9\u6cd5\u5c06 arr2 \u4e2d\u7684\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002<\/p>\n<p>\u6700\u7ec8\uff0cnewArr \u5c06\u5305\u542b arr1 \u548c arr2 \u4e2d\u7684\u6240\u6709\u5143\u7d20\uff1a<\/p>\n<pre>[1, 2, 3, 4, 5]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5c06\u5143\u7d20\u5408\u5e76\u6210\u6570\u7ec4\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\u4f7f\u7528 arrays.copyof() \u65b9\u6cd5\u5c06\u591a\u4e2a\u5143\u7d20\u5408\u5e76\u6210\u6570\u7ec4\uff0c\u6b65\u9aa4\u5982\u4e0b\uff1a\u4f7f\u7528 arrays.copyof() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u4e3a\u539f\u59cb\u6570\u7ec4\u5143\u7d20\u6570\u91cf\u4e4b\u548c\u7684\u65b0\u6570\u7ec4\u3002\u4f7f\u7528 system.arraycopy() \u65b9\u6cd5\u5c06\u5176\u4ed6\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\u65b0\u6570\u7ec4\u5c06\u5305\u542b\u6240\u6709\u539f\u59cb\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002 \u5982\u4f55\u4f7f\u7528 Java \u5c06\u5143\u7d20\u5408\u5e76\u6210\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5\u5c06\u591a\u4e2a\u5143\u7d20\u5408\u5e76\u6210\u4e00\u4e2a\u6570\u7ec4\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u539f\u59cb\u6570\u7ec4\u548c\u4e00\u4e2a\u65b0\u6570\u7ec4\u7684\u5927\u5c0f\uff08\u5143\u7d20\u6570\u91cf\uff09\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u539f\u59cb\u6570\u7ec4\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002 \u8bed\u6cd5\uff1a static &lt;T, U extends T&gt; T[] copyOf(T[] original, int newLength) \u767b\u5f55\u540e\u590d\u5236 \u53c2\u6570\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b original &#8211; \u8981\u5408\u5e76\u7684\u539f\u59cb\u6570\u7ec4 newLength &#8211; \u65b0\u6570\u7ec4\u7684\u5927\u5c0f\uff08\u5143\u7d20\u6570\u91cf\uff09 \u8fd4\u56de\u503c\uff1a \u4e00\u4e2a\u5305\u542b\u539f\u59cb\u6570\u7ec4\u5143\u7d20\u7684\u65b0\u6570\u7ec4 \u793a\u4f8b\uff1a \u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u6574\u6570\u6570\u7ec4\uff1a int[] arr1 = {1, 2, 3}; int[] arr2 = {4, 5}; \u767b\u5f55\u540e\u590d\u5236 \u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u8fd9\u4e24\u4e2a\u6570\u7ec4\u5408\u5e76\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff1a int[] newArr [&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-38865","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38865","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=38865"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38865\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}