{"id":38982,"date":"2024-11-26T15:17:41","date_gmt":"2024-11-26T07:17:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38982\/"},"modified":"2024-11-26T15:17:41","modified_gmt":"2024-11-26T07:17:41","slug":"%e6%80%8e%e4%b9%88%e7%94%a8java%e5%90%88%e5%b9%b6%e4%b8%a4%e4%b8%aa%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38982\/","title":{"rendered":"\u600e\u4e48\u7528java\u5408\u5e76\u4e24\u4e2a\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  java \u5408\u5e76\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528 system.arraycopy() \u65b9\u6cd5\u590d\u5236\u6570\u7ec4\u5185\u5bb9\u3002\u4f7f\u7528 guava \u5e93\u7684 concat() \u65b9\u6cd5\u3002\u4f7f\u7528 apache commons lang \u5e93\u7684 addall() \u65b9\u6cd5\u3002\u5229\u7528 java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u7684 stream api \u5408\u5e76\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111520582432531.jpg\" class=\"aligncenter\" title=\"\u600e\u4e48\u7528java\u5408\u5e76\u4e24\u4e2a\u6570\u7ec4\u63d2\u56fe\" alt=\"\u600e\u4e48\u7528java\u5408\u5e76\u4e24\u4e2a\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u5408\u5e76\u4e24\u4e2a\u6570\u7ec4<\/strong><\/p>\n<p>Java \u63d0\u4f9b\u4e86\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u4e24\u4e2a\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u65b0\u6570\u7ec4\uff1a<\/p>\n<p><strong>\u4f7f\u7528 System.arraycopy() \u65b9\u6cd5<\/strong><\/p>\n<p>\u8be5\u65b9\u6cd5\u5c06\u4e00\u4e2a\u6570\u7ec4\u7684\u90e8\u5206\u5185\u5bb9\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>System.arraycopy(sourceArray, sourceIndex, destinationArray, destinationIndex, length)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>sourceArray\uff1a\u8981\u590d\u5236\u7684\u6e90\u6570\u7ec4<\/li>\n<li>sourceIndex\uff1a\u8981\u590d\u5236\u7684\u6e90\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15<\/li>\n<li>destinationArray\uff1a\u590d\u5236\u76ee\u6807\u6570\u7ec4<\/li>\n<li>destinationIndex\uff1a\u590d\u5236\u76ee\u6807\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15<\/li>\n<li>length\uff1a\u8981\u590d\u5236\u7684\u5143\u7d20\u6570\u91cf<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] arr1 = {1, 2, 3};\nint[] arr2 = {4, 5, 6};\n\nint[] mergedArr = new int[arr1.length + arr2.length];\n\nSystem.arraycopy(arr1, 0, mergedArr, 0, arr1.length);\nSystem.arraycopy(arr2, 0, mergedArr, arr1.length, arr2.length);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Guava \u5e93<\/strong><\/p>\n<p>Guava \u5e93\u63d0\u4f9b\u4e86 Arrays \u7c7b\uff0c\u5176\u4e2d\u5305\u542b concat() \u65b9\u6cd5\u6765\u5408\u5e76\u4e24\u4e2a\u6570\u7ec4\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>int[] mergedArr = Ints.concat(arr1, arr2);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Apache Commons Lang \u5e93<\/strong><\/p>\n<p>Apache Commons Lang \u5e93\u63d0\u4f9b\u4e86 ArrayUtils \u7c7b\uff0c\u5176\u4e2d\u5305\u542b addAll() \u65b9\u6cd5\u6765\u5408\u5e76\u4e24\u4e2a\u6570\u7ec4\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>int[] mergedArr = ArrayUtils.addAll(arr1, arr2);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Java 8 \u53ca\u66f4\u9ad8\u7248\u672c<\/strong><\/p>\n<p>Java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u63d0\u4f9b\u4e86 Stream API\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5408\u5e76\u4e24\u4e2a\u6570\u7ec4\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>int[] mergedArr = Stream.concat(Arrays.stream(arr1), Arrays.stream(arr2)).toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u7528java\u5408\u5e76\u4e24\u4e2a\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 \u5408\u5e76\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528 system.arraycopy() \u65b9\u6cd5\u590d\u5236\u6570\u7ec4\u5185\u5bb9\u3002\u4f7f\u7528 guava \u5e93\u7684 concat() \u65b9\u6cd5\u3002\u4f7f\u7528 apache commons lang \u5e93\u7684 addall() \u65b9\u6cd5\u3002\u5229\u7528 java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u7684 stream api \u5408\u5e76\u6570\u7ec4\u3002 \u5982\u4f55\u4f7f\u7528 Java \u5408\u5e76\u4e24\u4e2a\u6570\u7ec4 Java \u63d0\u4f9b\u4e86\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u4e24\u4e2a\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u65b0\u6570\u7ec4\uff1a \u4f7f\u7528 System.arraycopy() \u65b9\u6cd5 \u8be5\u65b9\u6cd5\u5c06\u4e00\u4e2a\u6570\u7ec4\u7684\u90e8\u5206\u5185\u5bb9\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b System.arraycopy(sourceArray, sourceIndex, destinationArray, destinationIndex, length) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a sourceArray\uff1a\u8981\u590d\u5236\u7684\u6e90\u6570\u7ec4 sourceIndex\uff1a\u8981\u590d\u5236\u7684\u6e90\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15 destinationArray\uff1a\u590d\u5236\u76ee\u6807\u6570\u7ec4 destinationIndex\uff1a\u590d\u5236\u76ee\u6807\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15 length\uff1a\u8981\u590d\u5236\u7684\u5143\u7d20\u6570\u91cf \u793a\u4f8b\uff1a int[] arr1 = {1, 2, 3}; int[] arr2 = {4, 5, 6}; [&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-38982","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38982","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=38982"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38982\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}